Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView + iOS 7 / Xcode 5 = Assertion Failure

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>>)
like image 799
self.name Avatar asked Sep 26 '13 16:09

self.name


2 Answers

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;
}
like image 88
ZaBlanc Avatar answered Nov 09 '22 07:11

ZaBlanc


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

like image 6
MegaJiXiang Avatar answered Nov 09 '22 05:11

MegaJiXiang