Storyboard中collectionView分区头视图和尾视图的设置方法

一、在Storyboard中要进行的操作

Storyboard中,选择collection view controller中的“Collection View”。

在storyBoard中选中collectionView

Attributes inspector中,选择”Section Header”和”Section Footer”,一旦选中你就会在看到collection 展示出了他的Header和他的Footer.

在Attributes inspector选中Section Header和Section Footer

headerfooter之间默认为空,我们会用storyboard来设计视图。头部是专门用来显示一个部分的标题,而底部视图只显示静态横幅图片。

二、实现viewForSupplementaryElementOfKind方法

如果你尝试运行应用程序,你可能不会看到header和footer,这是因为我们还没有实现”viewFOrSupplementaryElementOfKind:”方法。

代码如下:

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

{

    UICollectionReusableView *reusableview = nil ;

    if (kind == UICollectionElementKindSectionHeader ){

        CollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind : UICollectionElementKindSectionHeader withReuseIdentifier : @”HeaderView” forIndexPath :indexPath];

        reusableview = headerView;

}

    if (kind == UICollectionElementKindSectionFooter){

        CollectionFooterView *footerview = [collectionView dequeueResuableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@”FooterView” forIndexPath:indexPath];

        reusableview = footerview;

}

    return reusableview;

}

上面的代码告诉它页眉/页脚视图应该在每个部分中使用collect view。我们首先确定该集合视图要求headerfooter view。这可以通过使用一种变量来完成。对于头来看,我们出列header view(使用dequeueReusableSupplementaryViewOfKind :方法),并设置适当的标题和图像。正如你可以从两个if之间的代码,我们使用我们之前分配给获得header/footer view标识符。

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

推荐阅读更多精彩内容

  • 转载地址:http://my.oschina.net/zboy/blog/221525 在前面我们已经学过,每个c...
    Ray_win阅读 7,245评论 0 2
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,251评论 4 61
  • 如果你还认为市场截图就是产品界面的截图效果,那请自觉补习小编往期的文章。今天我们只上干货,为大家罗列出几种主流截图...
    ASOPlus阅读 1,725评论 0 1
  • 上小学的时候有两只可爱的鸡宝宝,但都先后命丧黄泉了。都是我的错! 我一开始是很细心的照顾它们,见到它们长出羽毛来了...
    Messy珂阅读 360评论 0 0
  • 忘记是哪个月在网上看到有免费送《华夏家博会》的门票,想着正好要添几样家具,于是报名索取… 今天还真收到了文件袋,里...
    捡到蜜罐的熊阅读 166评论 0 0