美文网首页
Storyboard中collectionView分区头视图和尾

Storyboard中collectionView分区头视图和尾

作者: 韩吉_ | 来源:发表于2016-08-15 08:50 被阅读1152次

一、在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标识符。

相关文章

网友评论

      本文标题:Storyboard中collectionView分区头视图和尾

      本文链接:https://www.haomeiwen.com/subject/zmhusttx.html