【问题标题】:Error when adding Supplementary Header View to UICollectionView将补充标题视图添加到 UICollectionView 时出错
【发布时间】:2013-08-09 03:38:43
【问题描述】:

我在实现补充标题视图时遇到以下错误

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no UICollectionViewLayoutAttributes instance for -layoutAttributesForSupplementaryElementOfKind: HeaderView at path <NSIndexPath: 0x9e82a40> {length = 2, path = 0 - 0}'

这是创建标题视图的代码

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    static NSString * headerIdentifier = @"HeaderView";
    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:headerIdentifier withReuseIdentifier:UICollectionElementKindSectionHeader forIndexPath:indexPath];
    return header;
}

错误发生在 dequeueReusableSupplementaryViewOfKind 方法中。

我在 Collection View Controller 的 initWithCoder 中添加了这两行

UINib *headerNib = [UINib nibWithNibName:@"MTCollectionHeaderView" bundle:nil];
[self.collectionView registerNib:headerNib forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];

所以它确实有对 UI 元素的引用。我无论如何都找不到在标题上设置布局。

我尝试将其添加到同一个集合视图控制器中,但它从未命中此代码,正如我通过在其中放置调试器确认的那样

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return [self.collectionViewLayout layoutAttributesForDecorationViewOfKind:kind atIndexPath:indexPath];
}

有没有人看到这个问题,他们是如何解决的。我也在使用 XCode 5 Developer Preview 5 并为 iOS7 开发

【问题讨论】:

    标签: objective-c uicollectionview ios7 xcode5 uicollectionreusableview


    【解决方案1】:

    我相信您将 headerIdentifier 传递给补充视图的种类和标题标识符的种类常量。尝试按照collectionView:viewForSupplementaryElementOfKind:atIndexPath: 方法中的这行代码所示切换它们:

    UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];
    

    我也不认为你需要你的 layoutAttributesForSupplementaryElementOfKind:atIndexPath: 实现,把上面的代码行换进去看看它是否有效。

    【讨论】:

      猜你喜欢
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多