ios开发之--UICollectionView的使用
最近项目中需要实现一种布局,需要用到UICollectionView,特在此整理记录下!
贴上最终实现的效果图:

1,声明
@interface FirstViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *myCollectionView; @property(nonatomic,strong)UICollectionViewFlowLayout *myLayout;
2,创建
a,设置一个背景图片
UIImageView *imgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bgImg2.jpg"]];
imgV.frame = CGRectMake(, , KscreenW, KscreenH);
imgV.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view insertSubview:imgV atIndex:];
b,创建UICollectionView和FlowLayout
-(void)creatUI
{
self.myLayout = [[UICollectionViewFlowLayout alloc]init];
self.myLayout.minimumLineSpacing = ;
self.myLayout.minimumInteritemSpacing = ;
self.myLayout.itemSize = CGSizeMake(KscreenW/-, (KscreenW/)+);
self.myLayout.sectionInset = UIEdgeInsetsMake(, , , ); self.myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(, , KscreenW, KscreenH-) collectionViewLayout:self.myLayout];
self.myCollectionView.delegate = self;
self.myCollectionView.dataSource = self;
self.myCollectionView.backgroundColor = [UIColor clearColor];
// 注册cell
[self.myCollectionView registerNib:[UINib nibWithNibName:@"LeftCell" bundle:nil] forCellWithReuseIdentifier:@"LeftCell"];
[self.myCollectionView registerNib:[UINib nibWithNibName:@"RightCell" bundle:nil] forCellWithReuseIdentifier:@"RightCell"];
[self.myCollectionView registerNib:[UINib nibWithNibName:@"PublickCell" bundle:nil] forCellWithReuseIdentifier:@"PublickCell"];
// 注册头视图
[self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SECTION_ONE"];
[self.myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SECTION_TWO"]; [self.view insertSubview:self.myCollectionView atIndex:];
}
c,这里我用了三种自定义cell,上面的方法有具体的注册cell方法,还有头视图的注册

3,具体代理方法的实现
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return ;
} -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section == ) { return ;
}else{ return ;
}
} -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == ) { if (indexPath.item % == ) { LeftCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LeftCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor clearColor];
return cell;
}else{
RightCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RightCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
}else{ PublickCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PublickCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor]; return cell;
} return nil;
}
比较简单!
4,创建头视图,PS:这里需要注意个问题:多组头部视图样式不一样复用时发生错乱问题,代码如下:
a,注册头视图,有两种方法:
// 防止cell和头部视图复用出现错乱
[collectionView registerClass:[WOCOHomeSelectTypeCell class] forCellWithReuseIdentifier:@"SECTION_ONE"];
[collectionView registerClass:[WOCOHomeDisplayCell class] forCellWithReuseIdentifier:@"SECTION_TWO"];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SECTION_ONE"];
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SECTION_TWO"];
这两种方法都可以!
b,代理方法的实现:
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
// 防止重用,定义重用标识符
static NSString *reusableID;
if (indexPath.section == ) {
reusableID = @"SECTION_ONE";
}else{
reusableID = @"SECTION_TWO";
} UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reusableID forIndexPath:indexPath];
header.backgroundColor = [UIColor clearColor]; if (indexPath.section == ) {
headImg = [[UIImageView alloc]initWithFrame:CGRectMake(, , KscreenW, )];
headImg.image = [UIImage imageNamed:@"topLog"];
[header addSubview:headImg]; bottomImg = [[UIImageView alloc]initWithFrame:CGRectMake(KscreenW/-/, , , )];
bottomImg.image = [UIImage imageNamed:@"sj"];
[header addSubview:bottomImg]; }else{ UIImageView *bottomImg2 = [[UIImageView alloc]initWithFrame:CGRectMake(KscreenW/-/, , , )];
bottomImg2.image = [UIImage imageNamed:@"sj1"];
[header addSubview:bottomImg2];
} return header;
}
此方法设置了一个str,作为标识符对不同的section进行标记,这样就可以解决重用的问题,根据不同的id进入不同的section,避免了头视图上面的内容多次创建,各自创建各自的!
c,设置不同section的高度,只需要实现此代理方法即可:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
if (section == ) {
return CGSizeMake(KscreenW, );
}else{
return CGSizeMake(KscreenW, );
}
}
完成上面的操作,就可以在不同的section之间随意操作了!
ios开发之--UICollectionView的使用的更多相关文章
- ios开发――解决UICollectionView的cell间距与设置不符问题
在用UICollectionView展示数据时,有时我们希望将cell的间距调成一个我们想要的值,然后查API可以看到有这么一个属性: - (CGFloat)minimumInteritemSpaci ...
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
- iOS开发 纯代码创建UICollectionView
转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xi ...
- iOS开发 适配iOS10
2016年9月7日,苹果发布iOS 10.2016年9月14日,全新的操作系统iOS 10将正式上线. 作为开发者,如何适配iOS10呢? 1.Notification(通知) 自从Notificat ...
- IOS开发基础知识碎片-导航
1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...
- ios开发中的小技巧
在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...
- iOS开发 Xcode8中遇到的问题及改动
iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...
- iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件
本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
随机推荐
- CEdit 控制键盘操作
写在前面的话:在工作中要实现这样一个功能,在一个树形控件中选择要发送消息的客户,添加到一个列表控件中,点击确定跳到一个信息发送页面如下图,在发送人编辑框中显示选择的客户,要实现的就是即允许用户删除用户 ...
- ip_conntrack table full dropping packet解决方案
在一台繁忙的服务器上,建议关闭ip_conntrack模块的加载: 当我们开启iptables后,会有这么个现象发生,丢包.ping的话会断断续续的丢包,ifconfig 会看到网卡dropped:X ...
- c++之——虚析构函数
先看代码: #include<iostream> using namespace std; class Parent { public: Parent() :a(), b(), c() { ...
- rhel6.5 虚拟机的安装
一.准备: 在网上下载 rhel-server-6.5-x86_64-dvd.iso 并在 非 C盘下创建一个目录 ,比如:E:\VM\rhel_test 二. 创建虚拟机 进入 VMware , 点 ...
- pthread_once函数的简单示例
/*一次性初始化 int pthread_once(pthread_once_t *once_control, void (*init_routine) (void)) 本函数使用初值为PTHREAD ...
- ubuntu 12.10 默认安装php5-fpm无监听9000端口,nginx无法链接php5-fpm修正
升级php5的时候,发现nginx无法链接到php5,怀疑是php5端口的问题. netstat -an未发现监听9000端口. 查看/var/log/php5-fpm.log一切正常. 随后查看/e ...
- RRD.so文件 rrdruby
ubuntu 12.04绑定rrdruby gem install librrd 用来装rrdruby,这样才能找到RRD.so文件然后在rb文件中加入这两句话: $: << '/path ...
- div 边框
1.四个边框border-left 设置左边框,一般单独设置左边框样式使用border-right 设置右边框,一般单独设置右边框样式使用border-top 设置上边框,一般单独设置上边框样式使用b ...
- 【转】【WPF】IvalueConverter和TypeConverter
简要说明: IValueConverter主要用于XAML绑定和数据源之间的转换 TypeConverter主要用于自定义类的属性类型之间的转换 本文主要讲解如何使用IValueConverter和T ...
- Spring RestTemplate 小结
关于RestTemplate 首先,你可以把它理解为一个发起请求并接收响应的工具类(功能类似浏览器). 其次,它其实是一个壳,具体还是通过调用别的接口来实现(如jdk自带的连接,或者HttpClien ...