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

7

我的应用程序中有一个使用flowLayout的UICollectionView,在iOS6上运行得非常好,但在iOS7上则失败了。一旦我跳转到包含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>>)
7个回答

12

当我升级到iOS 7时,遇到了这个问题。最终问题的解决办法是,不要在dataSource中过于明确。如果你有以下内容,请移除:

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

完美的答案@ZaBlanc,谢谢!你有什么想法是iOS 6到7发生了什么变化吗?iOS 7 API差异没有显示除了添加到UICollectionView之外的任何内容。 - self.name

6

如果在这个函数中返回 nil,程序将会崩溃:

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

通常情况下,你需要返回 nil 的唯一原因是 "kind" NSString 不是你所期望的类型。在这种情况下,只需在界面生成器中删除该对象即可。我遇到了同样的崩溃问题,因为我的集合视图在界面生成器中有一个页脚但我没有调用 registerNib 代码(如 bneely 上面所描述的)来设置一个页脚。当我到达 viewForSupplementaryElementOfKind 并返回 nil 时,它是一种我不希望出现的类型(这肯定会导致崩溃)。


1

您需要在UICollectionView实例中注册一个UINib:

UINib *nib = [UINib nibWithNibName:@"YourNibNameWithoutExtension" bundle:nil];
[collectionView registerNib:nib forCellWithReuseIdentifier:@"YourReuseIdentifier"];

请通过-[UICollectionView dequeueReusableCellWithReuseIdentifier:forIndexPath:]创建所有的UICollectionViewCell实例。

Apple在UICollectionView.h中的注释解释了这个要求:

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

感谢@bneely,我已经注册了nibs(和类最初),但结果是ZaBlanc的答案解决了神秘崩溃。不要问我如何/为什么,因为在iOS7 API差异中,UICollectionView中没有真正过时的内容。 - self.name

1
我有一个问题,已经解决了。
我认为你可以检查一下IB Collection View中的Section Header -> Accessories -> Section Header是否已经被检查过了。

1
我收到了这个消息,是因为在界面构建器中忘记将我的类设置为正确的类型,以及未连接任何输出接口。

啊哈...这种情况也发生了!谢谢你的评论,让我检查了一下我新创建的可重用视图..现在内部不一致异常有意义了!:D - Pavan

0
你遇到这个错误是因为你的集合有一个标题。 我在IB中添加了一个标题后得到了这个错误。要么移除标题,要么检查标题选项的代理。

0
请确保您已经设置了集合视图的数据源和代理。在我的情况下,由于没有设置,导致出现了这个错误。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接