SecondiosAppTutorial--學習筆記,學習筆記app
自學SecondiosAppTutorial過半,運行程式報錯了,最終問題找到,英文文檔讀起來很有挫敗感,但耐心就能發現問題,Xcode的問題提示很明確清楚。如下:
/*Debug 輸出的報錯資訊,顯示TableView cell為空白值*/*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
tableView 沒有傳回值,cell 值為null。設計TableView的實現方法在MasterViewController.m中,先從代碼著手找問題。
1 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 2 2 /*UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 3 3 4 4 NSDate *object = self.objects[indexPath.row]; 5 5 cell.textLabel.text = [object description]; 6 6 return cell;*/ 7 7 static NSString *CellIdentifier = @"BirdSightingCell";/*BirdSightingCell是表格中cell的Identifier屬性名稱字*/ 8 8 9 9 static NSDateFormatter *formatter = nil;10 10 if (formatter == nil) {11 11 formatter = [[NSDateFormatter alloc] init];12 12 [formatter setDateStyle:NSDateFormatterMediumStyle];13 13 }14 14 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];15 15 NSLog(@"111222%@",cell);16 16 BirdSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row];17 17 [[cell textLabel] setText:sightingAtIndex.name];18 18 [[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)sightingAtIndex.date]];19 19 NSLog(@"111222%@",cell);20 20 return cell;21 21 }
cell沒有值,依次輸出資訊15、19輸出內容都是null。那麼第7行就很可能出錯了。BirdSightingCell是 TableView中cell的Identifier的屬性值,發現自己的表格中的值竟然是cell。恍然大悟。具體位置如下: