程序在dequeueReusableCellWithIdentifier崩溃:

Rut*_*cha 9 iphone xcode ios

我有一个程序,其中有一个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;
}
Run Code Online (Sandbox Code Playgroud)

崩溃日志显示:

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)
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的帮助.

Asi*_*eba 9

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

  • `dequeueReusableCellWithIdentifier:forIndexPath:`是一个完全有效的方法,从iOS 6.0开始. (5认同)
  • 谢谢cyberpawn ..它的工作原理.只需删除`forIndexPath:indexPath`,程序就开始工作了.当我创建一个子类为"UITableViewController"的新文件时,默认情况下会出现此方法.如果`forIndexPath:indexPath`是罪魁祸首,为什么它默认存在于程序中? (3认同)
  • 这个答案并没有解释为什么它崩溃,也没有错误.`dequeueResuableCellWithIdentifier:forIndexPath`完全有效,所以问题出在其他地方. (3认同)

rde*_*mar 7

您是否先注册了单元格或笔尖?

来自Apple文档:

要点:在调用此方法之前,必须使用registerNib:forCellReuseIdentifier:或registerClass:forCellReuseIdentifier:方法注册类或nib文件.

(顺便说一句,该方法仅适用于iOS 6)

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

在viewDidLoad中,在您的情况下注册该类,它只是UITableViewCell类:

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
Run Code Online (Sandbox Code Playgroud)

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

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.myArray[indexPath.row];
    return result;
}
Run Code Online (Sandbox Code Playgroud)