如何纯代码给UICollectionView添加HeaderView和FooterView

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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值