8

我有一个collectionview,我想使用xib 作为标题。但是,xib 不会出现。首先,我尝试向 xib 添加标签,但没有出现。然后我将整个背景颜色设置为红色。它没有出现。collectionview 项目为标题留下一个空白,但它是完全空白的。类似的 SO 线程是thisthis,但是由于我没有使用情节提要并且我的标题高度大于零,因此它们不能解决我的问题。这是我所有的相关代码:

#import "ScoreboardCollectionViewController.h"
#import "ScoreboardCollectionViewCell.h"
#import "ScoreboardReusableView.h"
#import "ScoreboardModel.h"
#import "Scoreboard.h"

...

- (void)viewDidLoad {
    [super viewDidLoad];

    // Register cell classes
    UINib *cellNib = [UINib nibWithNibName:@"ScoreboardCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:cellNib forCellWithReuseIdentifier:@"cell"];

    [self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];


    self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dark_fish_skin_"]]; 
}

...

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 34);
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
           viewForSupplementaryElementOfKind:(NSString *)kind
                                 atIndexPath:(NSIndexPath *)indexPath {

    ScoreboardReusableView *view = [[ScoreboardReusableView alloc] init];

    view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                                              withReuseIdentifier:@"scoreboardHeader"
                                                     forIndexPath:indexPath];

    return view;
}

这是我的xib的截图: 在此处输入图像描述

4

2 回答 2

19

好的,我解决了。问题是在我的viewDidLoad方法中我有

[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];

自从我使用笔尖以来,它应该是:

[self.collectionView registerNib:[UINib nibWithNibName:@"ScoreboardReusableView" bundle:nil]
          forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                 withReuseIdentifier:@"scoreboardHeader"];
于 2015-06-02T14:32:37.990 回答
0

我缺少 Collection View 的“Section Header”选项来启用:-

在此处输入图像描述

于 2020-12-21T21:45:06.533 回答