如何纯代码给UICollectionView添加HeaderView和FooterView
来源:互联网 发布:pat考试 知乎 编辑:程序博客网 时间:2024/06/03 07:30
collectionView和table基本用法一样但是header和footer,就找不到方法了
自己找了好久网上也没个人写的就一个写的是用storyBoard写的 对于纯代码的可能不怎么太理解:storyBoard的创建方法链接:http://my.oschina.net/u/723760/blog/221525。
自己摸索的做出来了。下面纯代码的步骤:<语文水平渣 没什么文采,但是技术点肯定会说明白的>
如果要给你的collectionView添加header和footer,他的数据源和代理是没有直接提供创建方法的,但是提供了一个两用的方法
<span style="font-size:14px;">- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath</span>
如果要给你的collectionView添加header和footer步骤<本文只以header为例>
1.设置流水布局 ,需要在流水布局里设置header和footer的size
- (id)init{ // UICollectionViewLayout; UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init]; // 流水布局
<span style="white-space:pre"></span>// 设置header的Size <span style="color:#ff6666;"> flow.headerReferenceSize = CGSizeMake(320, 44);</span>
// 设置格子的宽高 flow.itemSize = CGSizeMake(75, 61); // 设置列距 flow.minimumInteritemSpacing = 5; // 设置行距离 // flow.minimumLineSpacing = 0; // 设置整体内容和四周的边距 // top left bottom right flow.sectionInset = UIEdgeInsetsMake(20, 2, 0, 2); return [super initWithCollectionViewLayout:flow];}
<span style="font-size:14px;">#import <UIKit/UIKit.h>@interface UICollectionHeaderView : UICollectionReusableView@end</span>
4.在collectionViewController 的viewDidLoad注册xib,方法和注册cell差不多 只不过方法名不一样
<span style="font-size:14px;">UINib *nib = [UINib nibWithNibName:@"WdViewCell" bundle:nil] ; [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"cell"]; [self.collectionView setBackgroundColor:[UIColor colorWithRed:240 green:240 blue:240 alpha:0.8]]; </span>
<span style="font-size:14px;">// 注册header的 UINib *header = [UINib nibWithNibName:@"UICollectionHeaderView" bundle:nil]; [self.collectionView registerNib:header forSupplementaryViewOfKind:<span style="color:#ff6666;">UICollectionElementKindSectionHeader</span> withReuseIdentifier:@"header"];</span>
这个方法:kind标识你是header还是footer<可能还有其他的>header:UICollectionElementKindSectionHeader,用个判断或者switch就可以选择你要显示什么类容了,创建header view的方法
<span style="font-size:14px;">// 设置每组的标题- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ if (kind == UICollectionElementKindSectionHeader) { UICollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];</span>
<span style="font-size:14px;">// 在这就可以设置header中子控件的数据了</span>
<span style="font-size:14px;"> return headerView; } else { return nil; }}</span>这样就可以显示你的header了
本文到此结束,如果有什么解释不到或者错误的,还望大家指出,如果有什么不懂的可以直接M我,
0 0
- 如何纯代码给UICollectionView添加HeaderView和FooterView
- UICollectionView添加headerview/footerView
- UICollectionView Section 纯代码代码控制 HeaderView和FooterView的显示,uicollectionview
- UICollectionView Section 纯代码代码控制 HeaderView和FooterView的显示
- RecyclerView添加HeaderView和FooterView
- RecyclerView添加HeaderView和FooterView
- RecyclerView添加HeaderView和FooterView
- RecyclerView 添加headerView 和 footerView
- UICollectionView的HeaderView、FooterView
- UICollectionView headerView footerView
- RecyclerView封装--添加HeaderView和FooterView
- 为UICollectionView的每个section设置footerView和headerView
- 一 swift之UICollectionView纯代码cell的复用、footerView的添加
- 一个给RecyclerView添加HeaderView FooterView的装饰Adapter类
- 高性能的给RecyclerView加上HeaderView和FooterView
- RecyclerView优雅添加HeaderView FooterView
- ListView添加删除FooterView HeaderView
- ListView开发笔记之添加HeaderView和FooterView
- hdu 2103 Family planning
- poj1088 滑雪
- spring 链接 jdbc
- Broadcast类
- FreeBSD的netgraph
- 如何纯代码给UICollectionView添加HeaderView和FooterView
- Android中获取应用程序(包)的大小-----PackageManager的使用(二)
- 构建互联网iOS应用
- Java从pfx中获取公私钥
- ios 调用打电话 三种方式
- 安防监控体系解说
- QTranslate国际化及多语言切换
- 使用libcurl进行文件上传
- Bash的应用--4.通配符