iOS tableView、collectionView添加SectionHeader

      用过UITableView和UICollectionView的小伙伴都知道这两个view的继承自UIScrollView,因此也有着相类似的ViewDelegate和ViewDataSource,为了实现单元格Cell内容的个性话,我们可以自定义一个Cell类,然后在控制器中注册nib:

[self.tableView registerNib [UINib nibWithNibName: @"tableViewCell" bundle: nil] forCellReuseIdentifier: identifier ];

[self.collectionView registerNib: [UINib nibWithNibName:@"collectionViewCell" bundle: nil] forCellWithReuseIdentifier: identifier];

返回indexPath对应Cell可以用:

-(UITableViewCell*)tableView: (UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {...}

-(UICollectionViewCell*)collectionView: (UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {...}

        不过当我们要设计对应的HeaderView的时候,两者在实现上体现出了稍微大点的区别,先看tableView,当我们自定义一个view之后,就可以把view给对应的section:

- (UIView*)tableView:(UITableView*) tableView viewForHeaderInSection:(NSInteger) section {

if(section ==0) {

   return self.infoView;

}

   return nil;

}

然而collectionView中并没有viewForHeaderInSection方法,collectionView设置headerView, footerView的方法名略有特殊,是通过返回Supplementary element的View实现的。需要两个输入参数,一个是View的类型,UICollectionElementKindSectionHeader或UICollectionElementKindSectionFooter;另一个是所在的indexPath:

- (UICollectionReusableView*)collectionView: (UICollectionView*)collectionView viewForSupplementaryElementOfKind: (NSString*)kind atIndexPath: (NSIndexPath*)indexPath

{

UICollectionReusableView *reusableView =nil;

if(kind ==UICollectionElementKindSectionHeader){

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerView" forIndexPath: indexPath];

reusableView = headerView;

}

if(kind ==UICollectionElementKindSectionFooter) {

UICollectionReusableView *headerView =  [collectionView dequeueReusableSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerView" forIndexPath: indexPath];

reusableView = headerView;

}

return reusableView;

}

这里需要注意的有以下几点:

1. 如果自定义需要返回的headerView或footerView,不能继承自UIView,需要继承自UICollectionReusableView,并且一定需要返回非nil的reusableView,不然会报错。

所以建议都去判断kind为headerView或footerView时,都给reusableView赋非nil的reusableView。

2. 可以为自定义的UICollectionReusableView子类创建xib文件,并采用Collection Reusable View控件来布局,如果采用这种方式,一定需要在初始化UI的时候,注册该xib文件:

[self.collectionView registerNib:[UINib nibWithNibName: @"headerView" bundle: nil] forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: @"headerView"];

不然,collectionView仍然会添加headerView,不过却不会加载xib中的元素以及约束条件,看到的是一块空白区域,有兴趣可以试一下。

3. 如果仅仅是创建了继承自UICollectionReusableView的子类,但没有创建xib文件,而用代码添加元素及约束,一定要在初始化UI的时候,注册该子类:

[self.collectionView registerClass:[HeadView class]  forSupplementaryViewOfKind: UICollectionElementKindSectionHeader withReuseIdentifier: identifier];

不然会报错以下错误:

注册xib文件或者class可以和注册Cell的代码放在一起,例如viewDidLoad中。

4. 指定添加的headerView或footerView的大小可以用:

-(CGSize)collectionView: (UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection: (NSInteger)section{

CGSizesize={[UIScreen mainScreen].bounds.size.width,45};

return size;

}

注意以上几点,应该能够正确的添加collectionView的headerView或footerView了,有问题可以交流。没有单独的collectionView的Demo,包含在一个稍微完整的Demo中,链接在这里

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、UICollectionView介绍 UICollectionView和UICollectionViewCon...
    无沣阅读 9,997评论 4 18
  • 版权声明:未经本人允许,禁止转载. 1. TableView初始化 1.UITableView有两种风格:UITa...
    萧雪痕阅读 7,895评论 2 10
  • 作者唯一QQ:228544117; 一.简单介绍 UICollectionView 这个类是iOS6 新引进的AP...
    CC_iOS阅读 5,609评论 0 0
  • 概述 颈椎病又称颈椎综合征。是指颈椎及其周围软组织,如椎间盘、后纵韧带、黄韧带、脊髓鞘膜等发生病理改变而导致颈神经...
    白柠阅读 1,748评论 0 0
  • 日记里的日子 文/春儿 从十七岁开始,我把一个个日子,都装进了日记本。好像只有这样,才能证明,...
    幸福依然春儿阅读 1,800评论 2 2