質問

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>>)
役に立ちましたか?

解決

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;
}

他のヒント

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

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;

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

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

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top