【发布时间】:2013-09-30 22:04:12
【问题描述】:
我正在尝试将自定义视图添加到我的 UICollectionView 的标题部分。我有 xib 文件界面生成器,但我不使用情节提要。我在Interface Builder中检查了Section Header,但没有出现任何UICollectionReusableView,我该怎么办?
【问题讨论】:
标签: iphone ios uicollectionview uicollectionreusableview
我正在尝试将自定义视图添加到我的 UICollectionView 的标题部分。我有 xib 文件界面生成器,但我不使用情节提要。我在Interface Builder中检查了Section Header,但没有出现任何UICollectionReusableView,我该怎么办?
【问题讨论】:
标签: iphone ios uicollectionview 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;
}
【讨论】:
[self.selectionCollectionView registerNib:[UINib nibWithNibName:@"FlootThumbnailCollectionReusableView" bundle:nil] forCellWithReuseIdentifier:@"HeaderView"]; 而不是我改为 [self.selectionCollectionView registerNib:[UINib nibWithNibName:@"FlootThumbnailCollectionReusableView" bundle:nil] forCellWithReuseIdentifier:@"HeaderView"]