我一直在尝试在 Non-Storyboard iPad 项目中子类化 UICollectionReusableView。我在 IB 中构建了一个 View 并将其连接到我的自定义类,在我的 Collection View 所在的 viewController 中注册了该类以供重用,并在
中正确调用了它UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
但是,在 UICollectionView 的标题区域中没有显示任何内容。我想我也需要用编码器初始化 View ,但我不确定如何正确地做到这一点。我遵循了几个我发现的例子,但标题 View 仍然没有出现在我的 Collection View 中。
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[[NSBundle mainBundle] loadNibNamed:@"CVHeaderView" owner:self options:nil];
[self addSubview:self.categoryNameLabel];
}
return self;
谁能指出我正确的方向?
最佳答案
如果您使用 Storyboard并选择页眉/页脚复选标记 initWithCoder:
将被调用。
如果您不使用 Storyboard(或不单击页眉/页脚)而是手动连接它,您必须注册您的自定义类,然后 initWithFrame:
将被调用。
[self.collectionView registerClass:[GameCardCell class] forCellWithReuseIdentifier:@"GameCardCell"];
[self.collectionView registerClass:[PlayerHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PlayerHeaderView"];
[self.collectionView registerClass:[PlayerFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"PlayerFooterView"];
注意:两者都只会被调用一次。如果 View 从缓存中出来,将调用 prepareForReuse
。
关于ios6 - 子类化 UICollectionReusableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810742/