一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

ios开发之collectionview在Storyboard中设计Header和Footer的教程

时间:2015-08-23 编辑:简简单单 来源:一聚教程网

header和footer是视图流布局的补充。默认情况下,这些视图是在流布局中禁用的。不过可以通过下面几件事情来配置header和footer视图:

一、拖拽collectionview,并进行相关设置

到Storyboard中,选择collection view controller中的”Collection View”。在Attributes inspector中,选择”Section Header”和”Section Footer”,一旦选中你就会在看到collection 展示出了他的Header和他的Footer.B7A4A035-9845-47E3-B2EF-0E392E884841


B7A4A035-9845-47E3-B2EF-0E392E884841

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

二、实现viewForSupplementaryElementOfKind方法

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

代码如下:


- (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 *footerview = [collectionView dequeueResuableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
 
       reusableview = footerview;
 
   }
 
    return reusableview;
 
}
上面的代码告诉它页眉/页脚视图应该在每个部分中使用collect view。我们首先确定该集合视图要求header或footer view。这可以通过使用一种变量来完成。对于头来看,我们出列header view(使用dequeueReusableSupplementaryViewOfKind :方法),并设置适当的标题和图像。正如你可以从两个if之间的代码,我们使用我们之前分配给获得header/footer view标识符。

热门栏目