7

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>>)
bneely
  • 9,083
  • 4
  • 38
  • 46
self.name
  • 2,341
  • 2
  • 17
  • 18

7 Answers7

12

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;
}
ZaBlanc
  • 4,679
  • 1
  • 35
  • 38
  • Perfect answer @ZaBlanc, thank you! Any idea what changed from iOS 6 to 7? [The iOS 7 API Diffs](https://t.co/vuiz2TaU3r) don't show anything but additions to UICollectionView – self.name Oct 01 '13 at 23:45
6

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

Michael Sivolobov
  • 12,388
  • 3
  • 43
  • 64
1

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;
bneely
  • 9,083
  • 4
  • 38
  • 46
  • 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. – self.name Oct 02 '13 at 14:49
1

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

CRABOLO
  • 8,605
  • 39
  • 41
  • 68
xmkevinchen
  • 1,506
  • 1
  • 14
  • 17
1

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

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • 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 – Pavan Mar 03 '17 at 07:43
0

You get that error because your collection has a header. I got that after adding a header in IB. Remove the header or check the Delegate for header options

wolffan
  • 1,084
  • 10
  • 15
0

Be sure you have set collection view datasource and delegate. In my case this error was being thrown because of it.

Marcos Reboucas
  • 3,409
  • 1
  • 29
  • 35