OGeek|极客世界-中国程序员成长平台

标题: ios - UICollectionReusableView 不显示 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 18:19
标题: ios - UICollectionReusableView 不显示

我已将 添加到我的 Collection View 中,并如下设置我的 UICollectionView

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerReuseIdentifier];

我还添加了必要的方法。

- (CGSize)collectionViewUICollectionView *)collectionView layoutUICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSectionNSInteger)section {
    return CGSizeMake(self.view.frame.size.width, 44.0f);
}

- (UICollectionReusableView *)supplementaryViewForElementKindNSString *)elementKind atIndexPathNSIndexPath *)indexPath {
    NSLog(@"supplementaryViewForElementKind called");
    if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                                                               withReuseIdentifier:headerReuseIdentifier
                                                                                      forIndexPath:indexPath];
        header.backgroundColor = [UIColor redColor];
        return header;
    }

    return nil;
}

但是,supplementaryViewForElementKind 永远不会被调用。有什么想法吗?



Best Answer-推荐答案


如果您已经在 Interface Builder 中添加了标题 View 并且已经分配了类,那么您应该删除代码中的 registerClass 调用。它应该显示出来。中断您的委托(delegate)方法以查看它被调用。

关于ios - UICollectionReusableView 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45218690/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4