【问题标题】:XCode iOS - Assertion FailureXCode iOS - 断言失败
【发布时间】:2014-10-03 13:55:02
【问题描述】:

我刚刚开始学习 iOS 开发。我正在做“Start Developing iOS Apps Today”教程,我被困在“添加数据”部分。

在将表格视图设置为使用“动态原型”并将标识符设置为“ListPrototypeCell”后,我添加了“cellForRowAtIndexPath”方法,但它因以下错误而崩溃:

Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.1.sdk/System/Library/AccessibilityBundles/CertUIFramework.axbundle>(未加载)

2014-08-10 13:35:50.519 ToDoList[8954:60b] *** 断言失败 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-2935.137/UITableView.m:5439

2014-08-10 13:35:50.523 ToDoList[8954:60b] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 ListPrototypeCell 的单元格出列 - 必须注册一个 nib 或一个标识符的类或连接故事板中的原型单元'

。 . . .

libc++abi.dylib:以 NSException 类型的未捕获异常终止

我一直严格按照教程进行操作,但找不到错误。谁能建议我做错了什么?

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell" forIndexPath:indexPath];
    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    return cell;
}

【问题讨论】:

  • 您是否将视图控制器设置为放置此代码的类的名称?
  • 添加“异常断点”以停止导致此问题的行(应为 dequeueReusableCellWithIdentifier :)。检查您是否在正确的控制器中实现了该方法,并且在 Storyboard 表控制器中具有正确的类。

标签: ios xcode cocoa-touch


【解决方案1】:

用途:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListPrototypeCell"];

对于您想要的故事板。 如有必要,还可以通过以下方式跟进:

   if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ListPrototypeCell"];
    }

编辑:如果您想继续使用dequeueReusableCellWithIdentifier:forIndexPath:,那么在您的控制器初始化中您应该使用registerClass:forCellReuseIdentifier:,如果您不使用情节提要,请按照文档说明。在最新的 Xcode 版本中, (cell == nil) 部分也变得不必要了。

【讨论】:

  • 如果他正在使用带有动态表格视图单元格的情节提要,那么dequeueReusableCellWithIdentifier:forIndexPath: 应该可以正常工作,并且实际上是首选方式,因为它保证将返回一个单元格。
  • 我不这么认为。此外,如果他想使用该版本,他必须使用registerClass:forCellReuseIdentifier: 注册他的课程。所以这个答案是正确的。
  • 使用原型单元,您无需调用registerClass:forCellReuseIdentifier:registerNib:forCellReuseIdentifier:。它基本上是为你完成的。
  • 感谢@Rikkles,只要包含 if(cell == nil),代码 sn-p 就可以工作。但是你能解释一下为什么 Apple 教程中的代码不起作用吗?这是否意味着我在某个地方错过了一步?
  • 我认为您的问题是您使用的是哪个版本的 Xcode。在最新版本 6 中,您的代码应该按原样运行。 @DRBeardface 是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
  • 2011-05-27
  • 1970-01-01
  • 2018-01-14
相关资源
最近更新 更多