【问题标题】:Custom Header in UICollectionView with Interface Builder without StoryboardUICollectionView 中的自定义标题,带有不带情节提要的 Interface Builder
【发布时间】:2013-09-30 22:04:12
【问题描述】:

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

【问题讨论】:

    标签: iphone ios uicollectionview uicollectionreusableview


    【解决方案1】:

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

    【讨论】:

    • 我使用的是xib。你的解决方案对我有用。最初我尝试抵抗 NIB [self.selectionCollectionView registerNib:[UINib nibWithNibName:@"FlootThumbnailCollectionReusableView" bundle:nil] forCellWithReuseIdentifier:@"HeaderView"]; 而不是我改为 [self.selectionCollectionView registerNib:[UINib nibWithNibName:@"FlootThumbnailCollectionReusableView" bundle:nil] forCellWithReuseIdentifier:@"HeaderView"]
    猜你喜欢
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2012-04-03
    • 1970-01-01
    相关资源
    最近更新 更多