UI 中的自定义标头具有不带情节提要的界面生成器的视图



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

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

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

然后:

- (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;
}

最新更新