UICollectionView + iOS 7 / Xcode 5 = Assertion Failure

扶醉桌前 提交于 2019-12-04 21:54:27

问题


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

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



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




回答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;



回答4:


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




回答5:


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




回答6:


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




回答7:


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



来源:https://stackoverflow.com/questions/19034098/uicollectionview-ios-7-xcode-5-assertion-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!