【问题标题】:Program crashes at dequeueReusableCellWithIdentifier:程序在 dequeueReusableCellWithIdentifier 处崩溃:
【发布时间】:2012-11-06 04:29:51
【问题描述】:

我有一个程序,其中有一个 tableViewController,它有 1 节和 3 行。每当我运行该应用程序时,它总是在 dequeueReusableCellWithIdentifier: 方法处崩溃。我在这个方法中插入了一个断点和NSLog() 到零。我还将情节提要中原型单元格的Cell Identifier 设置为Cell。但程序仍然崩溃。这是导致问题的方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"indexPath.section = %d, indexPath.row = %d", indexPath.section, indexPath.row);
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil)
    {
        NSLog(@"in nil");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    return cell;
}

崩溃日志如下:

MyAppTake4[7463:707] indexPath.section = 0, indexPath.row = 0
2012-11-05 23:21:20.747 MyCardAppTake4[7463:707] -[UITableView     dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance     0xfbc9000
2012-11-05 23:21:20.753 MyCardAppTake4[7463:707] *** Terminating app due to uncaught     exception 'NSInvalidArgumentException', reason: '-[UITableView     dequeueReusableCellWithIdentifier:forIndexPath:]: unrecognized selector sent to instance     0xfbc9000'
*** First throw call stack:
(0x3254788f 0x3459d259 0x3254aa9b 0x32549915 0x324a4650 0x7e2f7 0x31fceefb 0x31fcdfd9     0x31fcd763 0x31f71f15 0x324a61fb 0x3420aaa5 0x3420a6bd 0x3420e843 0x3420e57f 0x342064b9     0x3251bb1b 0x32519d57 0x3251a0b1 0x3249d4a5 0x3249d36d 0x315f4439 0x31f9ccd5 0x7d9f9 0x7d994)
terminate called throwing an exception(lldb)

我有兴趣找出错误的确切原因,这样我就不会再犯这个错误了。

感谢您的帮助。

【问题讨论】:

  • iOS 6.0 中添加了UITableView 方法dequeueReusableCellWithIdentifier:forIndexPath:。您是否在 iOS 5.x 下收到此错误?

标签: iphone ios xcode


【解决方案1】:

那是因为 [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];在 iOS 6 中添加但 iOS 5 无法识别。为了兼容 iOS 5,请改用以下方法:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

【讨论】:

  • dequeueReusableCellWithIdentifier:forIndexPath: 从 iOS 6.0 开始是一个完全有效的方法。
  • 感谢cyberpawn .. 它奏效了。只是删除`forIndexPath:indexPath, the program started working. this method was present by default when I created a new file with subclass of UITableViewController. If forIndexPath:indexPath`是罪魁祸首,为什么它默认存在于程序中?
  • 由于您的评论帮助我首先确定了问题,因此我接受了您的回答。我也感谢其他人的参与! :-)
  • @Rut 谢谢,我认为您使用 Xcode 4.5 创建了您的项目,这就是默认为您创建它的原因。
  • 这个答案没有解释它为什么崩溃,也没有解释错误。 dequeueResuableCellWithIdentifier:forIndexPath 完全有效,所以问题出在其他地方。
【解决方案2】:

你是先注册cell的class还是nib的?

来自 Apple 文档:

重要提示:在调用此方法之前,您必须使用 registerNib:forCellReuseIdentifier: 或 registerClass:forCellReuseIdentifier: 方法注册类或 nib 文件。

(顺便说一句,该方法仅在 iOS 6 中可用)

如果您使用的是 iOS 6,那么您可以使用新的更简单的范例:

在 viewDidLoad 中,注册类,在你的情况下,这只是 UITableViewCell 类:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

然后,您可以在 cellForRowAtIndexPath 中这样做:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.myArray[indexPath.row];
    return result;
}

【讨论】:

  • 我已经注册了viewController的类。因为我使用的是故事板,所以没有nibcyberpawn 帮我找出了错误。感谢您的帮助。
  • 在我的viewDidLoad 中添加registerClass 方法对我有用。谢谢。
  • 这仅适用于特定的单元格实例,不适用于原型? :(
  • @ZackWeiner,不确定您的评论指的是什么。如果您在情节提要中制作单元格,则不应注册任何内容。如果单元格完全用代码制作,则注册类,如果单元格是在 xib 文件中制作,则注册 nib。
【解决方案3】:

由于 forIndexPath 方法是针对 ios6 的,我推荐你使用这个 snipet。在下面找到。

UITableViewCell *cell = [table_view dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    // **initialize** your stuff here **with their respected tag**
}
else
{
   // **get** your stuff back using their tags
}
// **assign** your valiues here

return cell;

享受编程

【讨论】:

    【解决方案4】:

    我有这个异常,并意识到它是由我的愚蠢错误引起的。我创建了一个空白的 .xib 文件(在我的 UITableView 中有一个自定义的“正在加载”单元格),但随后将 View 拖放到其中。呵呵!

    结果是您看到的异常消息,以及输出窗口中的以下内容:

    2014-04-25 20:45:12.953 Languages[36256:60b] *** Terminating app due to uncaught 
    exception 'NSInternalInconsistencyException', reason: 'invalid nib registered
    for identifier (LoadingCell) - nib must contain exactly one top level object 
    which must be a UITableViewCell instance'
    

    当然,解决方案是将Table View Cell 添加到我的 .xib 文件中,将其他 UI 组件复制到其中,然后将该表格视图单元格保留为文件中的“根”元素。

    【讨论】:

    • 是的,我不小心做了同样的事情。我的 .xib 文件很完美......但我错误地将标签拖放到主视图之外的 .xib 上。 (叹气。)又浪费了一个小时。
    【解决方案5】:

    导致此错误的另一个原因可能是-

    为标识符 (Cell) 注册的 nib 无效 - nib 必须恰好包含一个*对象,该对象必须是 UICollectionReusableView 实例

    我的 xib 中有一个 UIView 而不是 collectionViewCell。希望这会有所帮助。

    【讨论】: