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

Ios7 UICollectionView+;iOS 7/Xcode 5=断言失败,ios7,uicollectionview,xcode5,uncaught-exception,Ios7,Uicollectionview,Xcode5,Uncaught Exception,在我的应用程序中有一个使用flowLayout的UICollectionView,它在iOS 6中运行得很好,但在iOS 7中失败得很惨。一旦我转到包含UICollectionView的视图,就会发生以下情况: *** Assertion failure in -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:],

在我的应用程序中有一个使用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>>)
***在-[UICollectionView\u createPreparedSupplementaryViewForElementOfKind:atIndexPath:WithLayoutAttribute:applyAttributes:],/SourceCache/UIKit/UIKit-2903.2/UICollectionView.m:1401中断言失败
***由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因是:从-collectionView:ViewForSupplementalElementOfKind:atIndexPath返回的视图
(UICollectionElementKindSectionHeader,{length=2,path=0-0})未通过调用-dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:或为nil'检索到
()

您需要向UICollectionView实例注册UINib:

UINib *nib = [UINib nibWithNibName:@"YourNibNameWithoutExtension" bundle:nil];
[collectionView registerNib:nib forCellWithReuseIdentifier:@"YourReuseIdentifier"];
并通过
-[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:][/code>创建所有UICollectionViewCell实例

苹果UICollectionView.h中的这条评论解释了要求:

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

当我升级到iOS 7时,我遇到了这个问题。问题是,您不应该对数据源如此明确。如果您有以下内容,请将其删除:

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

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

- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath

基本上,您必须返回
nil
的唯一原因是如果“kind”
NSString
不是您期望的类型。在这种情况下,只需在interface builder中删除该对象。我也发生了同样的崩溃,因为我的集合视图在界面生成器中有一个页脚,但我没有调用
registerNib
code(如上所述)来设置页脚。我会进入SupplmentalElementOfKind的
视图,并返回nil,因为它是我没有预料到的一种类型。(这肯定会导致崩溃)。

由于集合有标题,所以会出现该错误。
我在IB中添加了一个标题后得到了这个问题。删除标题或检查代理的标题选项

我已经解决了这个问题

我想你可以查一下你查一下章节的标题
在IB Collection View->Accessories->Section Header中,我收到了这个消息,因为我忘记在interface builder中将我的类设置为正确的类型,并且没有连接和输出

请确保已设置Collection View数据源和委派。在我的例子中,抛出此错误是因为它。

完美答案@ZaBlanc,谢谢!知道从iOS 6到iOS 7发生了什么变化吗?只显示UICollectionViewThank@bneely的附加内容,我已经注册了NIB(以及最初的类),但事实证明ZaBlanc的答案修复了神秘的崩溃。不要问我怎么做/为什么,因为UICollectionView中的任何内容在iOS7 API diff.Aha中都没有被真正弃用。。。类似的事情也发生了!感谢您的评论,它让我检查了我新创建的可重用视图。。现在,内部不一致异常有意义了!:D