我有一个 Collection View ,我想使用一个 xib 作为标题。但是,xib 不会出现。首先我尝试给 xib 添加标签,但没有出现。然后我将整个背景颜色设置为红色。它没有出现。 collectionview 项目为标题留了一个空隙,但它完全是空白的。类似的 SO 线程是 this和 this ,但由于我没有使用 Storyboard并且我的页眉高度大于零,所以它们没有解决我的问题。这是我所有的相关代码:
#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 的截图:
最佳答案
好的,我解决了。问题是在我的 viewDidLoad
方法中我有
[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];
当我使用 Nib 时,它应该是:
[self.collectionView registerNib:[UINib nibWithNibName:@"ScoreboardReusableView" bundle:nil]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"scoreboardHeader"];
关于ios - UICollectionView header xib 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30583796/