xcode dequeueReusableCellWithReuseIdentifier 崩溃“无法使类型的视图出列” UICollectionElementKindCell'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13002711/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:37:10  来源:igfitidea点击:

dequeueReusableCellWithReuseIdentifier crash 'could not dequeue a view of kind" UICollectionElementKindCell'

xcodecollectionsdeque

提问by Ken

I am getting the following crash:

我遇到以下崩溃:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使类型视图出列:带有标识符 Cell 的 UICollectionElementKindCell - 必须为标识符注册一个笔尖或类,或者连接故事板中的原型单元格”

I do have the following in my ViewDidLoad:

我的 ViewDidLoad 中有以下内容:

[self.collectionView registerClass:[UICollectionViewCell class] 
        forCellWithReuseIdentifier:@"Cell"];

And the line that crashes is in the cellForItemAtIndexPath callback:

崩溃的行在 cellForItemAtIndexPath 回调中:

UICollectionViewCell *cell = [collectionView 
    dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];

I've been searching for hours but cannot find any solution. I've tried subclassing the UICollectionViewCell but get the same error.

我已经搜索了几个小时,但找不到任何解决方案。我试过对 UICollectionViewCell 进行子类化,但得到了同样的错误。

With breakpoints I have determined that the registerClass line is being executed before the dequeueReusableCellWithReuseIdentifier callback is executed.

通过断点,我确定在执行 dequeueReusableCellWithReuseIdentifier 回调之前正在执行 registerClass 行。

回答by Typo Johnson

I had this problem because I was calling registerClass beforeI was instantiating my table view object. Working code :

我遇到了这个问题,因为我实例化我的表视图对象之前调用了 registerClass 。工作代码:

self.tableView = [[UITableView alloc] initWithFrame:self.view.frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
[self.view addSubview:self.tableView];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView reloadData];

回答by Gorm

Also, don't forget the difference between a cell (register... forCellWithReuseIdentifier) and a supplementary view (register... forSupplementaryViewOfKind).

另外,不要忘记单元格(注册... forCellWithReuseIdentifier)和补充视图(注册... forSupplementaryViewOfKind)之间的区别。

I was dequeingit correctly with the supplementary (header/footer) type, but I accidentally registeredit as a cell type. Duh.

dequeing与补充(页眉/页脚)类型正确,但我不小心登记它作为细胞类型。呃。

回答by Enrico Susatyo

If you happen to experience this, in UICollectionViewControlleror UITableViewController, do as what rob mayoff said and make sure that your Collection Viewor Table Viewis hooked up property in your Storyboard.

如果您碰巧在UICollectionViewController或 中遇到这种情况UITableViewController,请按照 rob mayoff 所说的进行操作,并确保您的Collection ViewTable View与故事板中的属性相关联。

Another common mistake is in the Storyboard you forgot to give the right CollectionIDor CellID.

另一个常见的错误是在 Storyboard 中您忘记赋予权限CollectionIDCellID

回答by Narasimha Nallamsetty

Once when I was working on collection view cells, I wrote:

有一次我在处理集合视图单元格时,我写道:

static NSString *identifier = @"cell "; 

Looking carefully, you will see an extra space at the end of the identifier string. After realizing my mistake, I hope this will be helpful to someone.

仔细观察,您会在标识符字符串的末尾看到一个额外的空格。在意识到我的错误后,我希望这会对某人有所帮助。

Reference:

参考:

could not dequeue a view of kind: UICollectionElementKindCell with identifier

无法出列类型的视图:带有标识符的 UICollectionElementKindCell