【问题标题】:Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] error-[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] 错误中的断言失败
【发布时间】:2014-03-20 19:42:29
【问题描述】:

尝试填充我的表格时出现此错误:

* -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:] 中的断言失败

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *dictionary = tasks[indexPath.row];

    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = dictionary[@"key"];
    cell.detailTextLabel.text = [Global getPriority:(NSString *)dictionary[@"key_2"]];

    return cell;
}

当我删除 forIndexPath 时,错误消失,但我可以使用 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 方法。

有人可以帮帮我吗?

此外,该表是通过编程方式构建的。

谢谢,

彼得

【问题讨论】:

标签: ios objective-c uitableview


【解决方案1】:

dequeueReusableCellWithIdentifier:forIndexPath: 期望您之前已为相同的reuseIdentifier 注册了nibclass(或者您在nib/storyboard 中再次使用相同的@ 注册了prototype cell 987654329@。还保证此方法将始终返回有效单元格,因此如果您使用dequeueReusableCellWithIdentifier:forIndexPath: 方法,则无需以编程方式创建cell

如果您确实想以编程方式创建单元格,则应使用 dequeueReusableCellWithIdentifier 与您的 if (!cell) { cell =... } 代码配对。
欲了解更多信息,请查看this answer

【讨论】:

    【解决方案2】:

    我在代码中遇到了同样的问题。我发现我不小心将 UITableView 的委托附加到 View 控制器的视图中。将委托更改为 UIViewController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 2013-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-13
      • 2014-05-26
      相关资源
      最近更新 更多