UICollectionView + iOS 7 / Xcode 5 = Assertion Failure

12,250

Solution 1

When I updated to iOS 7 I ran into this. The problem ended up being that you shouldn't be so explicit with your dataSource. If you have the following, remove it:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

Solution 2

It will crash if you return nil in this function:

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

Basically, the only reason you would have to return nil is if the "kind" NSString is not a type you expect. In that case, just delete that object in the interface builder. I had this same crash because my collection view had a footer in the interface builder but I was not calling the registerNib code (as bneely described above) to set up a footer. I'd get to the viewForSupplementaryElementOfKind and return nil because it was a kind I was not expecting.(which is guaranteed to cause the crash).

Solution 3

You need to register a UINib with your UICollectionView instance:

UINib *nib = [UINib nibWithNibName:@"YourNibNameWithoutExtension" bundle:nil];
[collectionView registerNib:nib forCellWithReuseIdentifier:@"YourReuseIdentifier"];

And create all of your UICollectionViewCell instances via -[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:].

This comment in Apple's UICollectionView.h explains the requirement:

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

Solution 4

I received this by forgetting to set my class to the correct type in interface builder and not wiring up and outlets

Solution 5

I've got this problem, as solved

I think you could check have you check the Section Header in IB Collection View -> Accessories -> Section Header

Share:
12,250
self.name
Author by

self.name

Updated on August 05, 2022

Comments

  • self.name
    self.name almost 2 years

    In my app there was a UICollectionView using flowLayout and it was working beautifully in iOS 6 but fails horribly in iOS 7. As soon as I segue to the view containing my UICollectionView here's what happens:

    *** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:], /SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:1401
    *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath
    (UICollectionElementKindSectionHeader,<NSIndexPath: 0x145f3f50> {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil'
    (<UICollectionReusableView: 0x145f9400; frame = (0 0; 320 20); layer = <CALayer: 0x145f90c0>>)
    
  • self.name
    self.name over 10 years
    Perfect answer @ZaBlanc, thank you! Any idea what changed from iOS 6 to 7? The iOS 7 API Diffs don't show anything but additions to UICollectionView
  • self.name
    self.name over 10 years
    Thanks @bneely, I was already registering nibs (and classes initially) but it turns out ZaBlanc's answer fixed the mystery crash. Don't ask me how/why because nothing in UICollectionView was really deprecated in the iOS7 API Diffs.
  • Pavan
    Pavan about 7 years
    Aha... something like this happened too! Thank you for this comment, it made me check my newly created reusable view.. now the internal inconsistency exception makes sense! :D