SecondiosAppTutorial--学习笔记

自学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。恍然大悟。具体位置如下:

原文地址:https://www.cnblogs.com/liqiwa/p/5236056.html