UICollectionView Header xib没有显示

Jam*_*son 3 objective-c ios uicollectionview uicollectionreusableview

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

#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;
}
Run Code Online (Sandbox Code Playgroud)

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

Jam*_*son 11

好的,我解决了.问题在于我的viewDidLoad方法

[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];
Run Code Online (Sandbox Code Playgroud)

从我使用笔尖开始,应该是:

[self.collectionView registerNib:[UINib nibWithNibName:@"ScoreboardReusableView" bundle:nil]
          forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
                 withReuseIdentifier:@"scoreboardHeader"];
Run Code Online (Sandbox Code Playgroud)