collectionView怎么添加头视图
来源:互联网 发布:人工智能在线播放 编辑:程序博客网 时间:2024/04/30 13:24
经常有朋友在群里面问collectionView怎么添加头视图,不止一个问的,好多都在问,所以小编索性就写了一篇collectionView怎么添加头视图的博客,供大家参考学习。(示例代码下载地址)
我们要使用CollectionView里面的头视图需要先注册头视图 UICollectionReusableView或者 继承UICollectionReusableView的子类,kind类型为UICollectionElementKindSectionHeader,并且需要带一个标识符,我们定义个static 的静态字符串就行,如下所示:
[collectionView registerClass:[UICollectionReusableViewclass ] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
几个重要的不能忽视的点就是 UICollectionViewFlowLayout 布局属性需要设置 headerReferenceSize头部的大小,不然头视图没有大小不显示;一定要在
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath; 方法里面创建头视图view并给出frame,然后添加到 UICollectionReusableView 中。
详细代码如下<pre name="code" class="objc">//// HomeViewController.m// collection添加头部//// Created by user on 15/10/10.// Copyright (c) 2015年 user. All rights reserved.//#import "HomeViewController.h"#import "ConstomCell.h"static NSString *headerViewIdentifier = @"hederview";@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>@property (nonatomic,strong) UIImageView *headerImage;@end@implementation HomeViewController- (void)viewDidLoad { [super viewDidLoad]; //1.添加collectionview [self addCollectionView]; }-(void)addCollectionView{ UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; layout.minimumLineSpacing=20; //设置每一行的间距 layout.itemSize=CGSizeMake(100, 100); //设置每个单元格的大小 layout.sectionInset=UIEdgeInsetsMake(0, 0, 50, 0); layout.headerReferenceSize=CGSizeMake(self.view.frame.size.width, 250); //设置collectionView头视图的大小 UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout]; collectionView.frame=self.view.bounds; //注册cell单元格 [collectionView registerNib:[UINib nibWithNibName:@"ConstomCell" bundle:nil] forCellWithReuseIdentifier:@"cell"]; //注册头视图 [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier]; collectionView.backgroundColor=[UIColor whiteColor]; collectionView.delegate=self; collectionView.dataSource=self; [self.view addSubview:collectionView];}#pragma mark 返回多少行-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return 13;}#pragma markk 返回的单元格-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ ConstomCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; return cell;}// 返回头视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ //如果是头视图 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) { UICollectionReusableView *header=[collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerViewIdentifier forIndexPath:indexPath]; //添加头视图的内容 [self addContent]; //头视图添加view [header addSubview:self.headerImage]; return header; } //如果底部视图// if([kind isEqualToString:UICollectionElementKindSectionFooter]){// // } return nil;}/* * 补充头部内容 */-(void)addContent{ UIImageView *headerImage=[[UIImageView alloc]init]; headerImage.contentMode=UIViewContentModeScaleAspectFill; headerImage.clipsToBounds=YES; headerImage.frame=CGRectMake(0, 0, self.view.frame.size.width, 250); headerImage.image=[UIImage imageNamed:@"mei"]; self.headerImage=headerImage;}@end
代码的运行结果如下图
查看我的更多开源项目点击(开源项目)
- collectionView怎么添加头视图
- CollectionView瀑布流添加头视图,自定义Cell计算高度
- 在CollectionView添加头部视图
- 给collectionView设置头标题视图:
- CollectionView的HeaderView头视图悬停
- 给collectionView设置头标题视图
- collectionView头视图与组视图的创建
- CollectionView视图
- 给UICollectionView添加头视图
- 给webView添加头视图
- 代码添加UICollectionView添加头视图
- iOS--集合视图CollectionView
- 给UIWebView添加自定义头视图
- 给头视图添加个伸缩动画
- 谈谈UICollectionView添加头视图的方法
- collectionView添加头部view
- CollectionView 添加/删除动画
- CollectionView添加头部
- magento模块开发手册(一)Magento入门
- UE4蓝图案例:制作实时反射镜面材质
- 《剑指Offer》面试题:超过数组长度的一半的数
- MD5加密算法的原理和应用
- 黑马程序员--NSMutableArray的使用
- collectionView怎么添加头视图
- 避难向导
- Writing Fast Matlab code 6-7
- android学习:新浪微博Outh2授权,21322错误
- 舍本求末的运维自动化技术热潮
- 现代操作系统 -- 写在前面
- 驱动开发(6)NT驱动的基本结构
- LFI(本地文件包含)获取Webshell
- 黑马程序员——Java单例设计模式