我已经将<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>添加到我的集合视图中并设置了我的UICollectionView,如下所示。
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerReuseIdentifier];我还添加了必要的方法。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(self.view.frame.size.width, 44.0f);
}
- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath {
NSLog(@"supplementaryViewForElementKind called");
if ([elementKind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header = [self.collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:headerReuseIdentifier
forIndexPath:indexPath];
header.backgroundColor = [UIColor redColor];
return header;
}
return nil;
}然而,supplementaryViewForElementKind从未被调用过。有什么想法吗?
发布于 2017-12-13 02:24:16
如果您已经在接口生成器中添加了头视图,并且已经分配了类,那么您应该在代码中删除registerClass调用。那么它应该会显示出来。在你的委托方法中放一个断点,看看它是被调用的。
https://stackoverflow.com/questions/45218690
复制相似问题