我一直在尝试在一个非故事板iPad项目中创建UICollectionReusableView的子类。我已经在IB中构建了一个视图,并将其连接到我的自定义类,注册了这个类以便在我的集合视图所在的viewController中重用,并且在
UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath但是,在UICollectionView的标题区域中没有显示任何内容。我认为我需要用编码器初始化视图,但我不确定如何正确地这样做。我遵循了我找到的几个示例,但是header视图仍然没有出现在我的集合视图中。
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[[NSBundle mainBundle] loadNibNamed:@"CVHeaderView" owner:self options:nil];
[self addSubview:self.categoryNameLabel];
}
return self;}
有谁能给我指个方向吗?
发布于 2013-03-26 18:00:15
如果您使用情节提要并选择页眉/页脚复选标记,则将调用initWithCoder:。
如果您不使用情节提要(或不单击页眉/页脚),而是手动将其挂接起来,则必须注册您的自定义类,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"];注意:两者都只会被调用一次。如果视图是从缓存中取出的,就会调用prepareForReuse。
发布于 2012-11-02 16:09:17
在我的例子中,initWithFrame:在第一次出队时被自动调用。试着实现这个方法,看看它是否起作用。
https://stackoverflow.com/questions/12810742
复制相似问题