objective-c - 将补充标题 View 添加到 UICollectionView 时出错

标签 objective-c uicollectionview ios7 xcode5 uicollectionreusableview

我在执行补充页眉 View 时遇到以下错误

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

这是创建标题 View 的代码

- (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 元素的引用。无论如何我找不到在页眉上设置布局。

我尝试将其添加到同一个 Collection View Controller,但它从未命中此代码,正如我通过将调试器放在那里所确认的那样

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

有没有人见过这个问题,他们是如何解决的。此外,我正在使用 XCode 5 Developer Preview 5 并为 iOS7 开发

最佳答案

我相信您正在传递补充 View 类型的 headerIdentifier 和 header 标识符的类型常量。尝试按照 collectionView:viewForSupplementaryElementOfKind:atIndexPath: 方法中的这一行代码所示切换它们:

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

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

关于objective-c - 将补充标题 View 添加到 UICollectionView 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18139707/

相关文章:

iphone - dismissviewcontrolleranimated completion 不在 uiimagepickercontroller 上调用完成

ios - NSMutableData 到 NSDictionary 返回 null

ios - 如何在 UICollectionView、Swift 5、Xcode 的行下添加行

swift - 如何快速获取 Collection View 的标题?

iOS 7 MPVolumeView 改变大小

iOS7 XIB 问题。顶部和底部的空白

ios - 如何在它自己的线程中使用核心数据?

ios - 如何在 Objective C 中使用 JS Context SetObject

ios - 如何改变UICollectionView的填充方向?

ios7 - 旋转后 topLayoutGuide 未更新