简体   繁体   English

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

[英]Custom Header in UICollectionView with Interface Builder without Storyboard

I'm trying to add a custom view to the header section of my UICollectionView . 我正在尝试将自定义视图添加到我的UICollectionView的标题部分。 I have the xib file interface builder but I don't use storyboard. 我有xib文件界面构建器,但我不使用storyboard。 I have checked the Section Header in Interface Builder but doesn't appear any UICollectionReusableView, what can I do? 我已经检查了Interface Builder中的Section Header,但没有出现任何UICollectionReusableView,我该怎么办?

The easiest solution is to do is programatically (and keep your HeaderReusableView in another XIB file): 最简单的解决方案是以编程方式执行(并将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
}

And of course: 而且当然:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM