参考资料:http://www.myexception.cn/mobile/1856317.html //uicollectionview 纯代码格局, 添加头部视图
uicollectionview的基本使用这里就不写了 百度一大堆
自己写一个UIView 继承 UICollectionReusableView
@interface myHeadView : UICollectionReusableView { UILabel *TitleLable; } -(void)setTextTitle; @end
添加headview 实际上是 使用了uicollectionview的 SectionHeader来实现类似于 tableview 的 tableheaderview效果
[_myConllectionView registerClass:[myHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView"];
然后实现以下的协议
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; NSLog(@"kind = %@", kind); if (kind == UICollectionElementKindSectionHeader){ myHeadView *headerV = (myHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadView" forIndexPath:indexPath]; [headerV setTextTitle]; reusableview = headerV; } return reusableview; }