【问题标题】:UITableView dataSource must return a cell from tableView:cellForRowAtIndexPathUITableView dataSource 必须从 tableView:cellForRowAtIndexPath 返回一个单元格
【发布时间】:2026-02-22 04:55:02
【问题描述】:

我在https://*.com/search?q=UITableView+dataSource+must+return+a+cell+from+tableView%3AcellForRowAtIndexPath 进行了搜索,但我知道我需要检查单元格是否为零,但我的单元格是自定义的,我没有使用 xib,我使用的是故事板。我也选择了 CustomPlaceTableViewCell 类storyboard.as http://i.stack.imgur.com/g0rRO.png

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    //if ([cell isKindOfClass:[CustomPlaceTableViewCell class]]) { cell = (CustomPlaceTableViewCell *)cell; }
//retrieve the place info
Place *place = [self.placeDataController objectInPlaceListAtIndex:indexPath.row];

//set the cell with place info
if (place.name)
{
    cell.nameLabel.text =place.name;
}
else
{
    cell.nameLabel.text = @"PortAura";
}

 if (place.distance)
{
    cell.distanceLabel.text = place.distance;    
}
else
{
    cell.distanceLabel.text = @"PortAura";
}



 return cell;

}


CustomPlaceTableViewCell.m
@interface CustomPlaceTableViewCell : UITableViewCell{
  UILabel *nameLabel;
  UILabel *distanceLabel;
  UILabel *addressLabel;
}
@property (nonatomic) IBOutlet UILabel *nameLabel;
@property (nonatomic) IBOutlet UILabel *distanceLabel;
@property (nonatomic) IBOutlet UILabel *addressLabel;
 @end


   @implementation CustomPlaceTableViewCell
   @synthesize distanceLabel,nameLabel,addressLabel;

  - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
 {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
  }
 return self;
}

 - (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
  [super setSelected:selected animated:animated];

  // Configure the view for the selected state
 }

 @end

当我运行我的应用程序时,它会给我:

2012-07-02 17:16:26.675 MyAura[2595:707] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“UITableView 数据源必须从 tableView:cellForRowAtIndexPath: 返回一个单元格:”

【问题讨论】:

  • 我们没有收到您的问题,请说明清楚,我们可以为您做点什么......
  • 你能把完整的代码贴在你的 cellForRowAtIndexPath 里吗?
  • 当然你应该返回它return cell;
  • 我添加了返回单元格,没用

标签: iphone ios5


【解决方案1】:

我认为您错过了 tableView:cellForRowAtIndexPath 中的返回单元格 试试这个

return cell; 

检查或使用此NSLog(@"description = %@",[cell description]);

如果[cell description] 为空,则更改它

CustomPlaceTableViewCell *cell = (CustomPlaceTableViewCell*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

【讨论】:

  • CustomPlaceTableViewCell.xib 将类名从 UITableViewCell 更改为 CustomPlaceTableViewCell
  • 我没有使用 xib,我使用的是故事板,我已经修改了 CustomPlaceTableViewCell 选项
  • 嘿,请检查您的手机地址,您的手机地址为零,请再次检查
  • 使用这个 NSLog(@"description = %@",[cell description]);
【解决方案2】:

是的,即使您使用了上述所有建议,您的代码也会崩溃。您是否检查过您的单元格是否正在创建?

不,这并没有被创建,这个方法实际上返回了一个 nil 值。

在您的代码中使用以下行试图使用可重复使用的单元格,但是如果没有创建单元格怎么办?

CustomPlaceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

你必须在你的代码之后检查这个:

if (cell == nil) {
     cell = [[CustomPlaceTableViewCell alloc] init]; // or your custom initialization
}

在此之后,只有您的单元格不会为 nil,并且应用不会崩溃。

希望这能解决您的问题

【讨论】:

  • 通过我的应用程序没有崩溃,但视图没有出现任何数据,它给了我一个没有数据表
  • 你提到Terminating app due to uncaught exception 'NSInternalInconsistencyException'这就是我回答的原因。
  • 是的,但是如果我不使用自定义单元格,我使用的是系统单元格,一切正常,但我的标签不仅仅是系统单元格
  • @KapilChoubisa 没有必要检查是否创建了单元格,即 nil 与否,因为他正在使用情节提要。并且只需确保 CellIdentifier 名称与您在使用情节提要创建 tableview 时在属性中提供的名称相同。
  • 是的,使用情节提要创建tableview时,CellIdentifier与属性相同。
【解决方案3】:

我也遇到了同样的问题,但我解决了。

我正面临这个问题,因为我在单独的 Xib 文件中创建了我的 talbeViewCell,并将我的 tableview 类作为文件的所有者。但是,我还没有将我的单元格 IBOutlet 链接到我的 tableview 类。

因此,将我的 tableviewcell Xib 链接到在我的 tableview 中创建的 IBOutlet,我解决了这个问题。

您将遇到此问题,因为您在 cellforRowAtIndex 中创建的 tableviewcell 为 nil。

【讨论】:

    【解决方案4】:

    return cell; 结尾缺失

    【讨论】:

      【解决方案5】:

      我第一次使用:

          PlaceMasterViewController *placeController = [[PlaceMasterViewController alloc]init];
          [self.navigationController pushViewController:placeController animated:YES];
      

      导致我表中没有数据,或者给我错误,所以我使用了:

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard"
                                                                   bundle: nil];
      
          PlaceMasterViewController *placeController = (PlaceMasterViewController*)[mainStoryboard 
                                                             instantiateViewControllerWithIdentifier: @"peoplemastViewControl"];
          [self.navigationController pushViewController:placeController animated:YES];
      

      一切正常

      【讨论】:

        最近更新 更多