如何纯代码给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];}


2. 创建UICollectionReusableView的子类UICollectionHeaderView,并创建其xib 设置xib的identifier为header
<span style="font-size:14px;">#import <UIKit/UIKit.h>@interface UICollectionHeaderView : UICollectionReusableView@end</span>


3.在xib中添加你需要显示的控件,不要忘了class继承UICollectionHeaderView。


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>



5.显示header

这个方法: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
原创粉丝点击