ios 为标识符(CELLNAME)注册的无效笔尖 - 笔尖必须恰好包含一个顶级对象,该对象必须是 UITableViewCell 实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20442311/
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
invalid nib registered for identifier (CELLNAME) - nib must contain exactly one top level object which must be a UITableViewCell instance
提问by Cail Demetri
Sorry for the long title, but I wanted it to be clearly seen from a google search. Also, this differs from many of the other similar questions on here as its not specifying a 'null' identifier but one that actually exists.
对不起,标题太长,但我希望从谷歌搜索中可以清楚地看到它。此外,这与此处的许多其他类似问题不同,因为它没有指定“空”标识符,而是实际存在的标识符。
Essentially the problem occurs when your trying to navigate using one of the cells in a table view controller to segue to another view (Whatever that view may be).
本质上,当您尝试使用表视图控制器中的一个单元格导航到另一个视图(无论该视图可能是什么)时,就会出现问题。
Most people would run into this problem after gunning through the apple tutorial on the 'To-Do List' and expecting the cells to operate in the same motion regardless of their purpose. This problem is probably simplistic to most but for a beginner, its quite hard, it took me around 3 hours.
大多数人在浏览“待办事项列表”上的苹果教程并期望单元格无论其目的如何都以相同的动作运行后会遇到这个问题。这个问题对大多数人来说可能很简单,但对于初学者来说,它很难,我花了大约 3 个小时。
Basically the error is:
基本上错误是:
invalid nib registered for identifier (prototypeCell) - nib must contain exactly one top level object which must be a UITableViewCell instance
Where 'prototypeCell' would be whatever your cell is called. This is an exception that occurs immediately as the app is launched.
'prototypeCell' 将是您的单元格的名称。这是在应用程序启动时立即发生的异常。
回答by simon_smiley
I had the same problem as above but I wasn't using storyboards and the problem just appeared out of the blue.
我遇到了与上面相同的问题,但我没有使用故事板,问题只是突然出现。
I found that the solution was in the tableview cell file. I had added a uibutton, but it had been added outside the bounds of the cell by mistake. This meant it was almost like an extra view in the uiview.
我发现解决方案在 tableview 单元格文件中。我添加了一个 uibutton,但它被错误地添加到了单元格的边界之外。这意味着它几乎就像 uiview 中的一个额外视图。
Once I found it and deleted this extra view the problem disappeared immediately.
一旦我找到它并删除了这个额外的视图,问题立即消失了。
If you have having this error, check the uitableviewcell for extra views and objects added by mistake
如果您遇到此错误,请检查 uitableviewcell 以获取错误添加的额外视图和对象
回答by Jeehut
The answer of simon_smiley pointed me to the right direction, but some more trial-and-error was needed leading to the following solution:
simon_smiley 的回答为我指明了正确的方向,但需要更多的反复试验才能得到以下解决方案:
The problem doesn't only occur for additional top-level UIView objects, but also for gesture recognizers. So make sure not to use any gesture recognizers in your failing XIB file, you can set them up in code instead.
该问题不仅会出现在其他顶级 UIView 对象上,还会出现在手势识别器上。因此,请确保不要在失败的 XIB 文件中使用任何手势识别器,您可以改为在代码中设置它们。
For example you can do it in awakeFromNib
as noted by vahotm in the accepted answers comments.
例如,您可以awakeFromNib
按照 vahotm 在已接受答案评论中的说明进行操作。
回答by Annie
I had the same problem! And in my case custom cell was the subclass of UIView by mistake instead of UITableViewCell. So replacing UIView with UITableViewCell fixed the problem!
我有同样的问题!在我的情况下,自定义单元格错误地是 UIView 的子类,而不是 UITableViewCell。所以用 UITableViewCell 替换 UIView 解决了这个问题!
回答by William Hu
Same problem because I drag and drop a UITapGestureRecognizer
on the subviews of ContentView
. Just remove it.
同样的问题,因为我UITapGestureRecognizer
在ContentView
. 删除它。
回答by Ryan
I'll put up my dumb solution for the sake of googlers...
为了谷歌员工,我会提出我愚蠢的解决方案......
This was my personal dumb mistake - when I created a new custom cell for my table view, I went to the top level directory of my project -> add new file -> and created an Empty File type under the OS Xsection rather than the iOSsection.
这是我个人的愚蠢错误 - 当我为表格视图创建一个新的自定义单元格时,我转到了项目的顶级目录 -> 添加新文件 -> 并在OS X部分下创建了一个空文件类型而不是iOS部分。
Hopefully, most people have the other issue described above since it's less embarrassing :P
希望大多数人都有上述其他问题,因为它不那么令人尴尬:P
回答by WINSergey
Sometimes you using storyboard and have collectionView inside it and collectionView as well. After that you decide to simplified your Storyboard and divide cell into another nib. You create empty nib, Ctrl+C
from storyboard -> Ctrl+V
into nib.
有时您使用故事板并在其中包含 collectionView 和 collectionView 。之后,您决定简化故事板并将单元格分成另一个笔尖。您创建空笔尖,Ctrl+C
从故事板 ->Ctrl+V
到笔尖。
Everything looks fine but you'll have Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (PrettyCollectionViewCell) - nib must contain exactly one top level object which must be a UICollectionReusableView instance'
一切看起来都很好,但由于未捕获的异常“NSInternalInconsistencyException”,您将终止应用程序,原因:“为标识符注册的笔尖无效(PrettyCollectionViewCell)-笔尖必须恰好包含一个必须是 UICollectionReusableView 实例的顶级对象”
Ansver: do not do this. After I clean nib and add all of elements as I've in Storyboard - it fixed.
答:不要这样做。在我清洁笔尖并像我在 Storyboard 中一样添加所有元素之后 - 它修复了。
Seems like Bug of xCode IB - Version 7.3.1 (7D1014)
看起来像 xCode IB 的错误 - 版本 7.3.1 (7D1014)
回答by Cail Demetri
The problem is that there are multiple cells in your storyboard that have the same name. Such as for a single table view, there are multiple cells that have the same identifier. In my case I had three cells that were all called 'prototypeCell'.
问题是故事板中有多个具有相同名称的单元格。例如对于单个表视图,有多个具有相同标识符的单元格。在我的例子中,我有三个单元格,它们都被称为“prototypeCell”。
The fix is actually quite easy. For every cell, name it a simple name with the cell number at the end. This cell number has to match the indexPath.row later on, so basically start from 0 onwards.
修复实际上很容易。对于每个单元格,将其命名为一个简单的名称,并在末尾加上单元格编号。此单元格编号稍后必须与 indexPath.row 匹配,因此基本上从 0 开始。
For Example:
例如:
prototypeCell0
prototypeCell1
prototypeCell2
Then, go into the class thats responsible for the controller, and find the function
然后,进入负责控制器的类,找到函数
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Then replace the code:
然后替换代码:
static NSString *CellIdentifier = @"PrototypeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
With the code:
使用代码:
static NSString *CellIdentifier = @"ListPrototypeCell";
NSString* num = [NSString stringWithFormat:@"%d", indexPath.row];
NSString* actual = [CellIdentifier stringByAppendingString:num];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:actual forIndexPath:indexPath];
Everything else can stay the same. This code basically gets the row number, adds it to the identifier base, the gets the cell from the table using that string. So if its responding to a different row number, it return a different cell.
其他一切都可以保持不变。此代码基本上获取行号,将其添加到标识符库中,然后使用该字符串从表中获取单元格。所以如果它响应不同的行号,它会返回一个不同的单元格。
Just for clarification, my whole function for the problem is as follows:
只是为了澄清,我对这个问题的整个功能如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ListPrototypeCell";
NSString* num = [NSString stringWithFormat:@"%d", indexPath.row];
NSString* actual = [CellIdentifier stringByAppendingString:num];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:actual forIndexPath:indexPath];
cell.textLabel.text = self.content[indexPath.row];
return cell;
}
Good Luck!
祝你好运!
Also, this is my first Answer/Question combo, so I really have no idea how to approach it, so if i've done anything wrong, please tell me and/or change it. Thanks!
另外,这是我的第一个答案/问题组合,所以我真的不知道如何处理它,所以如果我做错了什么,请告诉我和/或更改它。谢谢!
回答by Zaid Pathan
回答by 2rahulsk
This error is mainly due to some extra views that are added by mistake. Have a look in the .xib file and check for extra unwanted view added by mistake. Delete that and it will work perfect. You can check for unwanted views by this is how it looks
这个错误主要是由于错误添加了一些额外的视图。查看 .xib 文件并检查错误添加的额外不需要的视图。删除它,它将完美运行。您可以通过它的外观来检查不需要的视图
回答by Andrew Kirna
I solved this by setting the class name in interface builder to match the cell reuse identifier (for that class) in interface builder.
我通过在接口构建器中设置类名来匹配接口构建器中的单元重用标识符(对于那个类)来解决这个问题。
Note: I'm not saying the class and identifier have to be the same. I'm saying they need to link the corresponding view and backing model.
注意:我并不是说类和标识符必须相同。我是说他们需要链接相应的视图和支持模型。