UICollectionView 中的自定义标头,带有 Interface Builder,无需 Storyboard

2024-02-19

我正在尝试将自定义视图添加到我的标题部分UICollectionView。我有 xib 文件界面生成器,但我不使用故事板。我已经检查了 Interface Builder 中的节标题,但没有出现任何 UICollectionReusableView,我该怎么办?


最简单的解决方案是以编程方式进行(并将 HeaderReusableView 保存在另一个 XIB 文件中):

[self.collectionView registerNib:[UINib nibWithNibName:@"ItemHeaderView" bundle:nil]
          forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                 withReuseIdentifier:kItemSectionHeaderViewID];

Then:

- (CGSize) collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(60.0f, 30.0f);// width is ignored
}

而且当然:

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

    NSString *productId = nil; ///

    ItemHeaderView *view = nil;

    view = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                       withReuseIdentifier:kItemSectionHeaderViewID
                                              forIndexPath:indexPath];

    view.titleLabel.text = productId;

    view.backgroundColor = [UIColor yellowColor];

    return view;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UICollectionView 中的自定义标头,带有 Interface Builder,无需 Storyboard 的相关文章

随机推荐