到Storyboard中,选择collection view controller中的"Collection View"。在Attributes inspector中,选择"Section Header"和"Section Footer",一旦选中你就会在屏幕中看到下面的的显示:

最重要的是,我们必须为header和footer view指定一个标识符。这个标示符将会被用于代码识别图片名称。在Atteributes inspector中设置header view的identifier为“HeaderView”,同样的把footer view的identifier设置为“FooterView”。

接下来为Header View 和Footer View添加新类

在默认情况下,header和footer view和UICollectionResuable类相关联。为了在header view中显示我们需要的内容,我们必须创建一个新的继承自UICollectionResuableView的类,我们可以命名为MyHeaderViewh和MyFooterView。

设置好相关Outlet

#import "MyHeaderView.h"

#import "MyFooterView.h"

// 定义 Header View Size

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

{

return CGSizeMake(0, 40);

}

// 定义 Footer View Size

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

{

return CGSizeMake(0, 40);

}

// 为collection view添加页眉或页脚

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

{

UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader){

MyHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group Header #%ld",indexPath.section +1];

headerView.headerTitle.text = title;

headerView.headerImageView.backgroundColor = [UIColor grayColor];

reusableview = headerView;

}

else if (kind == UICollectionElementKindSectionFooter){

MyFooterView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier forIndexPath:indexPath];

NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group Footer #%ld",indexPath.section +1];

footerview.footerTitle.text = title;

footerview.footerImageView.backgroundColor = [UIColor greenColor];

reusableview = footerview;

}

return reusableview;

}

// 一下是完整代码

#import "PhotoCollectionViewController.h"

#import "MyHeadView.h"

#import "MyFooterView.h"

@interface PhotoCollectionViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation PhotoCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

static NSString * const headerIdentifier = @"HeaderView";

static NSString * const footerIdentifier = @"FooterView";

- (void)viewDidLoad {

[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations

// self.clearsSelectionOnViewWillAppear = NO;

self.collectionView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];

// Register cell classes

[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];

NSMutableArray *array1  = [[NSMutableArray alloc]initWithObjects:@"image1.png",@"image2.png",@"image3.png",@"image4.png", nil];

NSMutableArray *array2  = [[NSMutableArray alloc]initWithObjects:@"image4.png",@"image3.png",@"image2.png", nil];

NSMutableArray *array3  = [[NSMutableArray alloc]initWithObjects:@"image1.png",@"image2.png", nil];

NSMutableArray *array4  = [[NSMutableArray alloc]initWithObjects:@"image1.png", nil];

_dataArray = [NSMutableArray arrayWithObjects:array1,array2,array3,array4, nil];

// Do any additional setup after loading the view.

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

#pragma mark <UICollectionViewDataSource>

// 返回collection view里区section的个数

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

return [_dataArray count];

}

// 返回指定区section包含的Items

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

return [[_dataArray objectAtIndex:section] count];

}

#pragma mark <UICollectionViewDelegate>

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

//重用cell

PhotoCollectionViewCell *myCell = [collectionView

dequeueReusableCellWithReuseIdentifier:@"photoCell"

forIndexPath:indexPath];

myCell.backgroundColor = [UIColor yellowColor];

UIImage *image = [UIImage imageNamed:[[_dataArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] ];

myCell.phontImageView.image = image;

return myCell;

}

// 定义每个UICollectionViewCell 的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

return CGSizeMake((self.view.frame.size.width-40)/3.0, (self.view.frame.size.width-40)/3.0);

}

// 定义每个Section 的 margin

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

return UIEdgeInsetsMake(20, 10, 5, 10);

}

// 每个section中不同的行之间的行间距

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{

return 10.0;

}

// 定义每个Section 的 margin

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section

{

return 10.0;

}

// indexPath处的item被选择时触发

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

NSLog(@"%ld",indexPath.row);

}

// 定义 Header View Size

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

{

return CGSizeMake(0, 40);

}

// 定义 Footer View Size

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

{

return CGSizeMake(0, 40);

}

// 为collection view添加页眉或页脚

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

{

UICollectionReusableView *reusableview = nil;

if (kind == UICollectionElementKindSectionHeader){

MyHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIdentifier forIndexPath:indexPath];

NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group Header #%ld",indexPath.section +1];

headerView.headerTitle.text = title;

headerView.headerImageView.backgroundColor = [UIColor grayColor];

reusableview = headerView;

}

else if (kind == UICollectionElementKindSectionFooter){

MyFooterView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerIdentifier forIndexPath:indexPath];

NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group Footer #%ld",indexPath.section +1];

footerview.footerTitle.text = title;

footerview.footerImageView.backgroundColor = [UIColor greenColor];

reusableview = footerview;

}

return reusableview;

}运行效果

转载于:https://www.cnblogs.com/joesen/p/4452850.html

StroyBoard中UICollectionView中添加Header和footer相关推荐

  1. RecyclerView添加header与footer

    前言 这次主要关于RecyclerView添加header和footer的实现方法,我们都知道,在使用ListView的时候我们能自由的给自己的ListView添加头部与尾部.使用addHeaderV ...

  2. 第七章:Paging添加header和footer

    paging使用:https://huangxiaoguo.blog.csdn.net/article/details/106567399 效果 封装可添加Header和Footer的BaseAdap ...

  3. 实现带header和footer功能的RecyclerView

    这个项目很简单,其实一年前就开发完成了,但是一直没闲下来去整理. RecyclerView是Android 5.0版本引入的一个新的组件,目的是在一些场景中取代之前ListView和GridView, ...

  4. swagger ui 怎么输入对象_java swagger ui 添加header请求头参数的方法

    我用到的swagger 主要有三款产品,swagger editor,swagger ui 和swagger codegen. swagger editor:主要是一个本地客户端,用来自己添加api, ...

  5. h5中的结构元素header、nav、article、aside、section、footer详解

    文章目录 header元素 nav元素 article元素 aside元素 section元素 footer元素 结构元素不具有任何样式,只是使页面元素的的语义更加明确 header元素 header ...

  6. Android listView 去掉header和footer中的分割线

    Android listView 去掉header和footer中的分割线 方法一: 在listView中加上android:headerDividersEnabled="false&quo ...

  7. jQuery Mobile中固定工具栏header、footer的data-*选项

    全栈工程师开发手册 (作者:栾鹏) jQuery Mobile 所有data-*选项 jQuery Mobile中固定工具栏header.footer的data-*选项 带有 data-role=&q ...

  8. SpringCloud 中 Feign 调用添加 Oauth2 Authorization Header

    SpringCloud 中 Feign 调用添加 Oauth2 Authorization Header SpringCloud 中通过 Feign 调用其他服务,当服务使用 Oauth2 授权的时候 ...

  9. 隐藏JqueryMobile中的Header与Footer

    <script type="text/javascript"> $(function () {//考虑你header和footer都存在var activePage = ...

最新文章

  1. PyTorch Hub发布获Yann LeCun强推!一行代码调用经典模型
  2. laravel安装笔记 (转)
  3. hdu 1016 Prime Ring Problem(DFS)
  4. php tostring(),【php】“__toString()”方法使用,php__tostring
  5. android listview edittext 事件,Android ListView item中含有Edittext 中一些坑
  6. 使用VS2010的Database项目模板统一管理数“.NET研究”据库对象
  7. 如何保证数据最终一致性(分布式事务)
  8. python中pandas有误_python-pandas to_sql方法给出日期列错误
  9. fseek函数与ftell函数使用例程
  10. wow修改人物模型_人物修改教程更仔细
  11. 这8款Android桌面插件,这款 Android 应用,帮你优雅地管理桌面小部件
  12. 小米机顶盒安装第三方软件流程
  13. 简约黑板擦特效表白网源码
  14. EntityFramework 的5种状态-简记
  15. cgcs2000大地坐标系地图_我国大地坐标系_地图与地图制图
  16. office2007之PPT模版更换与应用
  17. 雷军成功踏上移动互联网游轮
  18. 基于51单片机的LED呼吸灯设计(定时器)
  19. 习题 6.5 将一个数组中的值按逆序重新存放。例如,原来顺序为8,6,5,4,1。要求改为1,4,5,6,8。
  20. 双目视觉集合框架详解

热门文章

  1. dev GridControl双击行事件
  2. es6 --- Thunk函数的作用
  3. Python与操作系统有关的模块
  4. Python版——博客网站四 编写日志创建页
  5. 视频图像不正常的几个表现及解决方法
  6. BroadcastReceiver应用详解(转)
  7. ntop网络监控-安装配置指南
  8. 《鸟哥的linux私房菜-服务器篇 第三版》 RHCA亲授
  9. HTTP详解(转载)
  10. python第三方库安装的各种方法(全网最全,最简单易懂)