ios - 无法使 UICollectionView header 正常工作

标签 ios objective-c uicollectionview uicollectionreusableview

相关代码如下:

Controller :

- (instancetype)init {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    layout.itemSize = CGSizeMake(106.0, 106.0);
    layout.minimumInteritemSpacing = 1.0;
    layout.minimumLineSpacing = 1.0;
    layout.headerReferenceSize = CGSizeMake(320.0, 44.0);

    return (self = [super initWithCollectionViewLayout:layout]);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // some setup

    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self.collectionView registerClass:[ITPhotosHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

}

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

    if (kind == UICollectionElementKindSectionHeader) {
        ITPhotosHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                          withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerView = [[ITPhotosHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
        reusableView = headerView;
    }

    return reusableView;
}

这是我得到的错误:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'the view returned from -collectionView:viewForSupplementaryElementOfKind:atIndexPath (UICollectionElementKindSectionHeader,<NSIndexPath: 0xc000000000000016> {length = 2, path = 0 - 0}) was not retrieved by calling -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath: or is nil (<ITPhotosHeaderView: 0x1781b9de0; baseClass = UICollectionReusableView; frame = (0 0; 320 44); layer = <CALayer: 0x17802f980>>)

我调试并确保它没有返回 nil。因此我觉得它的 registerClass 部分不能正常工作。我将不胜感激任何意见。谢谢。

最佳答案

这里有一个明确的问题:

    ITPhotosHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                      withReuseIdentifier:@"header" forIndexPath:indexPath];
    headerView = [[ITPhotosHeaderView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];

该代码毫无意义。在第一行中,您将标题 View 出队。在第二行中,您肆意丢弃刚刚出列的标题 View 并创建一个全新的标题 View 。

关于ios - 无法使 UICollectionView header 正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737052/

相关文章:

ios - UICollectionView 具有两个 UICollectionViewFlowLayout

iphone - iOS 使用 PullToRefreshView 使 UICollectionView 可滚动

ios - 使用 cocoapods 将 pod 添加到通用框架库

iOS Storyboard - 导航 Controller 不工作

objective-c - NSPredicate代替循环来过滤对象数组

iphone - 不同实体的多个 NSFetchedResultControllers?

ios - 在iOS App中下载多个图像

ios - 为 Xcode 6 iPhone 模拟器移除 iOS 8 UITableView 上的 SeparatorInset

objective-c - 如何解决 Core Foundation/IO Kit 中较新的多 GPU Apple 笔记本电脑上的 CGDirectDisplayID 更改问题?

swift - 当滚动甚至存储在 NSCache 中时,UIImage 一直保持加载 - swift