【问题标题】:UICollectionView + iOS 7 / Xcode 5 = Assertion FailureUICollectionView + iOS 7 / Xcode 5 = 断言失败
【发布时间】:2013-09-26 16:52:48
【问题描述】:

在我的应用程序中有一个使用 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>>)

【问题讨论】:

    标签: ios7 uicollectionview xcode5 uncaught-exception


    【解决方案1】:

    当我更新到 iOS 7 时,我遇到了这个问题。问题最终是您不应该对数据源如此明确。如果您有以下情况,请将其删除:

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

    【讨论】:

    • 完美答案@ZaBlanc,谢谢!知道从 iOS 6 到 7 的变化吗? The iOS 7 API Diffs 除了添加到 UICollectionView 之外什么都不显示
    【解决方案2】:

    如果你在这个函数中返回nil,它会崩溃:

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

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

    【讨论】:

      【解决方案3】:

      您需要在您的 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,我已经在注册笔尖(和最初的课程),但事实证明 ZaBlanc 的回答修复了神秘的崩溃。不要问我如何/为什么,因为 UICollectionView 中的任何内容都没有在 iOS7 API Diffs 中真正被弃用。
      【解决方案4】:

      我遇到了这个问题,已经解决了

      我想你可以检查一下你是否检查了节标题 在 IB Collection View -> Accessories -> Section Header

      【讨论】:

        【解决方案5】:

        我收到这个是因为忘记在界面生成器中将我的类设置为正确的类型,而不是连接和插座

        【讨论】:

        • 啊哈……类似的事情也发生了!感谢您的评论,它让我检查了我新创建的可重用视图.. 现在内部不一致异常是有道理的! :D
        【解决方案6】:

        您收到该错误是因为您的集合有标题。 我在 IB 中添加标题后得到了这个。删除标题或检查标题选项的委托

        【讨论】:

          【解决方案7】:

          确保您已设置集合视图数据源和委托。在我的情况下,这个错误是因为它而被抛出的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-10-03
            • 2012-09-17
            • 1970-01-01
            • 2015-10-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多