我已经在情节提要中创建了 UICollectionView 并添加了页眉页脚视图,它工作正常。但我的问题是如何创建 UICollectionViewReusable 视图以编程方式添加为 SupplementaryView。我试过但没有调用委托。请注意我也设置了委托。下面的代码我有试过了
- (void)setUpCustomCollectionView
{
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 40, 320, 500) collectionViewLayout:layout];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView"];
self.collectionView.bounces = NO;
self.collectionView.tag = 10;
self.collectionView.backgroundColor = [UIColor darkGrayColor];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
self.collectionView.dataSource=self;
self.collectionView.delegate=self;
[self.baseScrollView addSubview:self.collectionView];
}
在委托中
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath
{
if (kind == UICollectionElementKindSectionHeader) {
UICollectionReusableView *headerView = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"brandingHeaderView" forIndexPath:indexPath];
UIView * view =[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 80)];
view.backgroundColor = [UIColor redColor];
[headerView addSubview:view];
return headerView;
}
}
引导我。