在使用UICollectionView的时候,我们需要设置三个代理,分别是UICollectionDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource。
注册头视图:(跟注册cell类似)
①. //注册头视图
[collectionView registerClass:[HXHBabyHeaderModfiy class]
forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
withReuseIdentifier:HEADERID];
②.//创建头视图UICollectionViewDataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
viewForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath *)indexPath {
//注释: HXHBabyHeaderModfiy类要继承UICollectionReusableView
HXHBabyHeaderModfiy *
=[collectionView dequeueReusableSupplementaryViewOfKind :UICollectionElementKindSectionHeaderwithReuseIdentifier:HEADERID
forIndexPath:indexPath];
return _headerView;
}
③.// 设置section头视图的参考大小,与tableheaderview类似 UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {
if (section == 0) {
return CGSizeMake(HXHScreen_Width, 450);
}
else {
return CGSizeMake(0, 0);
}
}