我有一个collectionview,我想使用xib作为标题。但是,xib不会出现。首先,我尝试在xib中添加标签,但没有出现。然后我将整个背景颜色设置为红色。它没有出现。 collectionview项目为标题留下了空白,但它完全是空白的。类似的SO线程是this和this,但由于我没有使用故事板而我的标题高度大于零,因此它们无法解决我的问题。这是我的所有相关代码:
#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的屏幕截图:
答案 0 :(得分:11)
viewDidLoad
方法
[self.collectionView registerClass:[ScoreboardReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"scoreboardHeader"];
当我使用笔尖时,应该是:
[self.collectionView registerNib:[UINib nibWithNibName:@"ScoreboardReusableView" bundle:nil]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:@"scoreboardHeader"];
答案 1 :(得分:0)