【问题标题】:UICollectionReusableView not showingUICollectionReusableView 不显示
【发布时间】:2017-07-20 15:09:37
【问题描述】:

我已将<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> 添加到我的收藏视图并设置我的UICollectionView 如下。

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

我还添加了必要的方法。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(self.view.frame.size.width, 44.0f);
}

- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)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 永远不会被调用。有什么想法吗?

【问题讨论】:

  • 你在 viewdidload 中添加了 self.collectionView.delegate = self 吗?
  • 是的,对不起。我忘了把它添加到问题中。
  • 你在实现uicollectionviewdelegate中注册到delegate了吗?
  • @StefanSzekeres 是的,我做到了。代表就是我注册到<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>UICollectionViewController

标签: ios uicollectionview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2014-01-21
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多