【问题标题】:Assertion failure in UITableView configureCellForDisplay:forIndexPath:UITableView configureCellForDisplay:forIndexPath 中的断言失败:
【发布时间】:2013-02-25 00:18:53
【问题描述】:

查看其他类似问题后,我不确定错误出在哪里。 我收到了一个断言失败。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:

我认为这很简单,但希望有人能提供帮助。

下面是我的代码:

#import "StockMarketViewController.h"

@interface StockMarketViewController ()

@end


@implementation StockMarketViewController
@synthesize ShareNameText, ShareValueText, AmountText;
@synthesize shares, shareValues;


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return [shares count];

}

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



    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;

}

【问题讨论】:

  • 请更新您的问题以标记导致异常的行。如果您不知道,请设置断点并单步执行代码,直到找到确切的行。
  • 嗯,我是 xcode 的新手,所以我不确定这些东西,但在这种情况下,没有出现错误,并且当我选择相关页面时程序运行但崩溃?所以我不知道哪一行是错误的,如果这是有道理的?我想 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  • 实际上是否有一个带有(区分大小写)标识符@"cell"的单元格?
  • 如果不是这样会导致问题吗?正如我所说,我对此很陌生,并且正在使用教程来帮助我,这是他们使用的代码的一部分。
  • 如果有帮助,请更新问题,谢谢。

标签: ios objective-c cocoa-touch uitableview


【解决方案1】:

您永远不会创建单元格,您只是尝试重用已出列的单元格。但由于您从未创建过,所以没有。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }

    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
}

或尝试(仅限 iOS 6+)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    NSString *currentValue = [shareValues objectAtIndex:[indexPath row]];
    [[cell textLabel]setText:currentValue];
    return cell;
}

来自 UITableView.h

- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier;  // Used by the delegate to acquire an already allocated cell, in lieu of allocating a new one.
- (id)dequeueReusableCellWithIdentifier:(NSString *)identifier 
                           forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // newer dequeue method guarantees a cell is returned and resized properly, assuming identifier is registered

-dequeueReusableCellWithIdentifier: 总是需要检查,如果返回了一个单元格,而
-dequeueReusableCellWithIdentifier:forIndexPath: 可以实例化一个新单元格。

【讨论】:

  • 他的做法完全没问题。如果单元格不存在,dequeueReusableCellWithIdentifier 将创建该单元格。
  • @sapi 不是这样,它只会在您使用故事板原型或已注册 nib 或类以供重用时创建一个单元格。
  • 是的,但如果他正在学习教程,那么他极有可能正在使用原型(或正在尝试,但尚未定义)。这也是 UITableViewController 的默认代码存根所期望的。
  • @sapi,你的意思是 - (id)dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(6_0); // 较新的出队方法保证单元格被返回并正确调整大小,假设标识符已注册
  • 我确实是这个意思。抱歉这个错误,也许我应该睡一觉:)
【解决方案2】:

如果您尚未在 Storyboard 中定义标识符为 @"cell" 的原型单元格,则在尝试将其出列时会收到断言错误。

您可以通过在原型单元格上设置Identifier 属性来解决此问题(选择单元格并在右侧面板中设置该属性)。

【讨论】:

  • 这听起来可能令人震惊,但我没有使用故事板?我还能做些什么吗?为什么人们投票反对我的问题,建议如何改进?因为它很难从非编程背景开始使用这种语言。谢谢
  • 如果您不使用 Storyboard,请参阅 @jrturton 对 vikingosegundo 答案的评论。
  • 谢谢。我不小心设置了“恢复 ID”。在身份检查器中以“单元格”而不是属性检查中的“标识符”。
【解决方案3】:

我犯的一个非常愚蠢的错误是

我没有把 UITableViewDelegate、UITableViewDataSource 放在控制器类名之后,比如 我的课程代码是 类 TagsViewController:UIViewController

它应该有 class TagsViewController: UIViewController , UITableViewDelegate, UITableViewDataSource

可能是你们中的一个人因为这个而面临的所有其他代码都可以。

【讨论】:

  • 谢谢,犯了同样的错误。解决了!
  • 好@HardikDarji
【解决方案4】:

您需要在自定义TableViewCell中调用“initWithStyle”并重新初始化对象。

示例:ProductTableViewCell.m 文件

@implementation ProductTableViewCell

- (void)awakeFromNib {
}

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

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _titleLabel = [[UILabel alloc] initWithFrame:(CGRectMake(70, 0, 320, 60))];
        [self.contentView addSubview:_titleLabel];
   }
   return self;
}

在主实现文件中

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    ProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productTableViewCell"];
    NSDictionary *dic = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        dic = [_filteredArray objectAtIndex:indexPath.row];
    } else {
        dic = [_originalArray objectAtIndex:indexPath.row];
    }
    cell.titleLabel.text = [dic objectForKey: @"title"];
    return cell;
}

【讨论】:

    【解决方案5】:

    我有同样的错误,我设法找到了错误。我有一个用于 segues 和视图标题的数组:

    NSArray *MMTitles= [NSArray arrayWithObjects:@"MainMenu",@"viewIt",@"viewNots",@"MyProfile",@"Settings",@"Instructions",@"Help", nil];
    NSArray *MMSegues=[NSArray arrayWithObjects:@"MainMenu",@"MyProfileSegue",@"viewNotSegue",@"MyProfileSegue",@"SettingsTableViewSegue",@"InstructionsViewSegue",@"HelpViewSegue", nil];
    
    self.menuItems = [[NSArray alloc]initWithObjects:MMTitles,MMSegues, nil];
    

    然后我使用这个数组作为我的表的数据源。我收到的错误是因为我在实例化 VC 时实际上并没有在我的 Storyboard 中声明 HelpViewSegue

        vc = [mainStoryboard instantiateViewControllerWithIdentifier: [[self.menuItems objectAtIndex:1]objectAtIndex:indexPath.row]];
    

    非常琐碎,但非常令人沮丧!希望这会有所帮助。

    【讨论】:

      【解决方案6】:

      在下面的代码中你写了@"cell"(用小号c写),但你必须使用@"Cell"C必须是大写)。

      - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
      {
          UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
      

      【讨论】:

      • 也添加索引路径
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 2014-05-26
      • 2012-04-25
      • 2014-11-19
      • 1970-01-01
      相关资源
      最近更新 更多