【发布时间】:2017-07-20 15:09:37
【问题描述】:
我已将<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 永远不会被调用。有什么想法吗?
【问题讨论】:
-
你在 viewdidload 中添加了 self.collectionView.delegate = self 吗?
-
是的,对不起。我忘了把它添加到问题中。
-
你在实现uicollectionviewdelegate中注册到delegate了吗?
-
@StefanSzekeres 是的,我做到了。代表就是我注册到
<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>的UICollectionViewController。
标签: ios uicollectionview