4

实施补充标题视图时出现以下错误

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 开发

4

1 回答 1

6

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

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

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

于 2013-08-09T05:56:25.500 回答