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

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

最简单的解决方案是以编程方式执行(并将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; }