29

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?

Fogmeister
  • 76,236
  • 42
  • 207
  • 306

7 Answers7

37

You need to register like below:

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MY_CELL"];
Gaurav
  • 8,227
  • 4
  • 34
  • 55
29

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

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • When do you call this method? on the line before "UICollectionViewCell *cell = [collectionView dequeue..." or in a different method – Jordan Medlock Oct 08 '12 at 16:57
  • 1
    I had to register it in the viewDidLoad method. You only need to register the xib once for the whole collectionView. Then when you call dequeueCellWithIdentifier it goes to the xib you registered. – Fogmeister Oct 08 '12 at 18:05
  • 2
    I had the same problem, but for some odd reason the compiler didn't recognize the `registerClass` method in `viewDidLoad` so I had to move it to the `cellForItemAtIndexPath` method. – Sam Spencer Oct 14 '12 at 13:39
  • @Sam: The reason for this may be that your `UICollectionView` object wasn't allocated yet when you called `registerClass:forCellWithReuseIdentifier:` method in `viewDidLoad`. This should not happen if your collection view is defined via Interface Builder as `viewDidLoad` is called **after** initializing the collection view then. In case you initialize your collection view programmatically just ensure that it gets allocated **before** you call the `registerClass:...` method on it. I don't think `cellForItemAtIndexPath` is the perfect place for it ... – CGee Jan 13 '14 at 10:12
  • @Sam I had the same problem and the reason was that the ` cellForItemAtIndexPath` was called in `[super viewDidLoad]` when I've moved `registerNib:...` before `[super viewDidLoad]` it worked just fine. – Zuzana Paulis Feb 21 '16 at 18:59
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.

Jeffrey Basurto
  • 1,425
  • 1
  • 10
  • 24
U.Jhon
  • 41
  • 3
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.

bshirley
  • 8,217
  • 1
  • 37
  • 43
1

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

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

enter image description here

heyfrank
  • 5,291
  • 3
  • 32
  • 46
0

Replace

NSString *CellIdentifier = @"Cell";

with

static NSString *CellIdentifier = @"Cell";
CAMOBAP
  • 5,523
  • 8
  • 58
  • 93
  • Tried that first. I've just been reading the docs. I need to register a class or nib before running dequeue... – Fogmeister Sep 26 '12 at 10:42
0

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;
Chris Klingler
  • 5,258
  • 2
  • 37
  • 43