iOS Assertion Failure in UICollectionView

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

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.

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?

回答1:

You need to register like below:

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


回答2:

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...

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

Thanks



回答3:

Make sure that if you use the registerNib: method:

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

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



回答4:

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

Move

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

to be in - (void)viewDidLoad,

rather than method - (void)awakeFromNib.



回答5:

Replace

NSString *CellIdentifier = @"Cell";

with

static NSString *CellIdentifier = @"Cell";


回答6:

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:

[_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.

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

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;


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!