UICollectionView中的自定义标题,带有没有Storyboard的Interface Builder

Pie*_*ero 5 iphone ios uicollectionview uicollectionreusableview

我正在尝试将自定义视图添加到我的标题部分UICollectionView.我有xib文件界面构建器,但我不使用storyboard.我已经检查了Interface Builder中的Section Header,但没有出现任何UICollectionReusableView,我该怎么办?

mdz*_*iec 12

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

[self.collectionView registerNib:[UINib nibWithNibName:@"ItemHeaderView" bundle:nil]
          forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                 withReuseIdentifier:kItemSectionHeaderViewID];
Run Code Online (Sandbox Code Playgroud)

然后:

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

而且当然:

- (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;
}
Run Code Online (Sandbox Code Playgroud)