UICollectionView + iOS 7/Xcode 5 =断言失败

sel*_*ame 7 uncaught-exception uicollectionview ios7 xcode5

在我的应用程序中有一个使用flowLayout的UICollectionView,它在iOS 6中工作得非常漂亮,但在iOS 7中失败了.只要我看到包含我的UICollectionView的视图,就会发生以下情况:

*** 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>>)
Run Code Online (Sandbox Code Playgroud)

ZaB*_*anc 12

当我更新到iOS 7时,我遇到了这个问题.问题最终是你不应该对你的dataSource这么明确.如果您有以下内容,请将其删除:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您返回nil此函数,它将崩溃:

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

基本上,你必须返回的唯一原因nil是"种类" NSString不是你期望的类型.在这种情况下,只需在界面构建器中删除该对象即可.我遇到了同样的崩溃,因为我的集合视图在界面构建器中有一个页脚,但我没有调用registerNib代码(如上所述,如上所述)来设置页脚.我会到达viewForSupplementaryElementOfKind并返回零,因为它是一种我没想到的.(这可以保证导致崩溃).