xcode 程序在 dequeueReusableCellWithIdentifier 处崩溃:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13244403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Program crashes at dequeueReusableCellWithIdentifier:
提问by Rutvij Kotecha
I have a program in which there is a tableViewController which has 1 section and 3 rows. Whenever I run the app, it always crashes at dequeueReusableCellWithIdentifier:
method. I inserted a breakpoint and NSLog()
to zero in the this method. I also set the Cell Identifier
of the prototype cell in storyboard to Cell
. But the program still crashes. Here's an method that is causing the problem:
我有一个程序,其中有一个 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;
}
The crash log reads:
崩溃日志如下:
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)
I am interested in identifying the exact cause of error so that I do not make this mistake again.
我对确定错误的确切原因很感兴趣,这样我就不会再犯这个错误了。
Thanks for your help.
谢谢你的帮助。
采纳答案by Asif Mujteba
Thats because [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; was added in iOS 6 but not recognized by iOS 5. For iOS 5 compatibility use below method instead:
那是因为 [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 已在 iOS 6 中添加但不被 iOS 5 识别。对于 iOS 5 兼容性,请改用以下方法:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
回答by rdelmar
Did you register the class of the cell or the nib first?
你是先注册单元类还是笔尖类?
From the Apple docs:
来自苹果文档:
Important: You must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method.
重要提示:在调用此方法之前,您必须使用 registerNib:forCellReuseIdentifier: 或 registerClass:forCellReuseIdentifier: 方法注册一个类或 nib 文件。
(btw that method is only available in iOS 6)
(顺便说一句,该方法仅适用于 iOS 6)
If you're using iOS 6, then you can use the new simpler paradigm:
如果您使用的是 iOS 6,那么您可以使用新的更简单的范例:
In viewDidLoad, register the class, in your case, that's just the UITableViewCell class:
在 viewDidLoad 中,注册该类,在您的情况下,这只是 UITableViewCell 类:
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
Then, you can just do it like this in cellForRowAtIndexPath:
然后,您可以在 cellForRowAtIndexPath 中这样做:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.myArray[indexPath.row];
return result;
}
回答by Niru Mukund Shah
Since forIndexPath method is for ios6, I recommend you to use this snipet. Find below.
由于 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;
Enjoy Programming
享受编程
回答by Mike Gledhill
I had this exception, and realised that it was caused by my stupid mistake. I had created a blank .xib file (to have a custom "Loading" cell in my UITableView
), but had then drag'n'dropped a View
into it. Doh!
我有这个例外,并意识到这是由我的愚蠢错误引起的。我创造了一个空白的.xib文件(有一个自定义的“加载”单元格在我的UITableView
),但后来drag'n'dropped一View
进去。呸!
The result was the exception message which you're seeing, plus this in the Output window:
结果是您看到的异常消息,以及输出窗口中的:
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'
The solution, of course, was to add a Table View Cell
to my .xib file, copy the other UI components into it, and just leave that Table View Cell as the "root" element in the file.
当然,解决方案是将 a 添加Table View Cell
到我的 .xib 文件中,将其他 UI 组件复制到其中,然后将该表视图单元格保留为文件中的“根”元素。
回答by AJain
Another cause for this error can be-
此错误的另一个原因可能是-
invalid nib registered for identifier (Cell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance
为标识符(单元格)注册的无效笔尖 - 笔尖必须恰好包含一个顶级对象,该对象必须是 UICollectionReusableView 实例
I had a UIView in my xib instead of a collectionViewCell. Hope this helps.
我的 xib 中有一个 UIView 而不是 collectionViewCell。希望这可以帮助。