UICollectionView 中的 iOS 断言失败

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

iOS Assertion Failure in UICollectionView

iphoneiosuicollectionview

提问by Fogmeister

I'm getting the error ...

我收到错误...

*** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:], /SourceCache/UIKit/UIKit-2372/UICollectionView.m:2249

When trying to display a UICollectionView.

尝试显示 UICollectionView 时。

The lines causing it are...

导致它的线路是...

static NSString *CellIdentifier = @"Cell";

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

Error happening on the dequeue.

出队时发生错误。

There are no other errors so I'm struggling to know where to begin with this.

没有其他错误,所以我很难知道从哪里开始。

Can anyone shed light on this?

任何人都可以阐明这一点吗?

回答by Gaurav

You need to register like below:

您需要像下面这样注册:

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

回答by Fogmeister

Been reading the docs (should possibly have done this first :) )

一直在阅读文档(可能应该先这样做:))

Anyway, the collectionView I am using is within a separate xib file (not a storyboard) and from the docs...

无论如何,我使用的 collectionView 位于一个单独的 xib 文件(不是故事板)和文档中...

Important: You must register a class or nib file using the
registerClass:forCellWithReuseIdentifier: or
registerNib:forCellWithReuseIdentifier: method before calling this method.

Thanks

谢谢

回答by U.Jhon

I had the same problem. Here's how I solved it.

我有同样的问题。这是我解决它的方法。

Move

移动

[self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]

[self.pictureCollectionView registerNib:[UINib nibWithNibName: bundle:nil] forCellWithReuseIdentifier:reuseID]

to be in - (void)viewDidLoad,

- (void)viewDidLoad

rather than method - (void)awakeFromNib.

而不是方法- (void)awakeFromNib

回答by bshirley

Make sure that if you use the registerNib:method:

确保如果您使用该registerNib:方法:

UINib *nibH = [UINib nibWithNibName:HEADER_ID bundle:nil];
[collectionView registerNib:nibH
 forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
        withReuseIdentifier:HEADER_ID];

that ALSOin the nib file, when you select the top-level collection reusable view, use the attributes inspector, and make surethe Identifieris set to the same value you are passing in to the withReuseIdentifier:parameter.

ALSO在笔尖文件,当您选择顶级集合可重复使用的视图中,使用属性检查器中,并确保Identifier设置为您传递到相同的值withReuseIdentifier:参数。

回答by fl034

I got this crash on iOS 9 only(iOS 10/11 are working fine).

只在 iOS 9 上遇到了这个崩溃(iOS 10/11 工作正常)。

I had no custom subclass of a Flow Layout but setting the headerReferenceSizeon the existing one directly. So in Interface Builder with Section Headerenabled I got this crash, without the checkmark everything works fineand the headers are being displayed correctly, since I set the size in code.

我没有 Flow Layout 的自定义子类,而是headerReferenceSize直接在现有的子类上设置。所以在启用了部分标题的界面生成器中,我遇到了这个崩溃,没有复选标记,一切正常,标题显示正确,因为我在代码中设置了大小。

enter image description here

在此处输入图片说明

回答by ColossalChris

I have seen this error pop up when using multiple UICollectionViews with unique ReuseIdentifiers. In ViewDidLoad you want to register each CollectionView's reuseIdentifier like so:

当使用多个带有唯一 ReuseIdentifiers 的 UICollectionViews 时,我看到这个错误弹出。在 ViewDidLoad 中,您希望像这样注册每个 CollectionView 的重用标识符:

[_collectionView1 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView1CellIdentifier"];
[_collectionView2 registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collectionView2CellIdentifier"];

Then when you get to "- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath" you want to make sure that you don't try to set a cell for collectionView1 to the reuseIdentifier for collectionView2 or you will get this error.

然后,当您到达“- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath”时,您要确保不要尝试将 collectionView1 的单元格设置为 collectionView2 的重用标识符,或者您会得到这个错误。

DON'T DO THIS: (Or collectionView2 will see the wrong Identifier and throw a fit before seeing the identifier it was expecting)

不要这样做:(或者 collectionView2 会看到错误的标识符并在看到它期望的标识符之前抛出一个合适的)

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

if(collectionView != _collectionView1){
   cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
}

cell.backgroundColor = [UIColor greenColor];
return cell;

DO THIS:

这样做

UICollectionViewCell *cell;

if(collectionView == _collectionView1){
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView1CellIdentifier" forIndexPath:indexPath];
}else{
   cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionView2CellIdentifier" forIndexPath:indexPath];
}

cell.backgroundColor = [UIColor greenColor];
return cell;

回答by CAMOBAP

Replace

代替

NSString *CellIdentifier = @"Cell";

with

static NSString *CellIdentifier = @"Cell";