最近项目中需要实现一种布局,需要用到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的使用的更多相关文章

  1. ios开发――解决UICollectionView的cell间距与设置不符问题

    在用UICollectionView展示数据时,有时我们希望将cell的间距调成一个我们想要的值,然后查API可以看到有这么一个属性: - (CGFloat)minimumInteritemSpaci ...

  2. iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流

    上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...

  3. iOS开发 纯代码创建UICollectionView

    转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xi ...

  4. iOS开发 适配iOS10

    2016年9月7日,苹果发布iOS 10.2016年9月14日,全新的操作系统iOS 10将正式上线. 作为开发者,如何适配iOS10呢? 1.Notification(通知) 自从Notificat ...

  5. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  6. ios开发中的小技巧

    在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...

  7. iOS开发 Xcode8中遇到的问题及改动

      iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...

  8. iOS开发之窥探UICollectionViewController(五) --一款炫酷的图片浏览组件

    本篇博客应该算的上CollectionView的高级应用了,从iOS开发之窥探UICollectionViewController(一)到今天的(五),可谓是由浅入深的窥探了一下UICollectio ...

  9. iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流

    在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...

随机推荐

  1. Ribbon负载均衡策略与自定义配置

    Ribbon负载均衡策略 配置 对调用的某个服务启用某种负载策略 1)通过配置文件配置 hello: ribbon: NFLoadBalancerRuleClassName:com.netflix.l ...

  2. C#中分别对委托、匿名方法、Lambda表达式、Lambda表达式树以及反射执行同一方法的过程进行比较。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. pyqt布局管理器

    有时候,碰到错误: QWidget::setLayout: Attempting to set QLayout “” on MainWindow “”, which already has a lay ...

  4. java比较字符串相等

    java中String是对象类型,不能使用"=="比较.正确的用法如下: if(A.equals(B)){ //相等 }

  5. 推荐10款纯css3实现的实用按钮

    在2014年的双11即将来临之季,爱编程小编为大家整理10款纯css3实现的按钮.希望这对坚守在前端的码农们有所帮助.亲,如果你有好的资源也可在本文留言,让从事编码的程序员们抱团.工作更轻松. No1 ...

  6. linux下getrlimit与sysconf函数

    #include <stdio.h> #include <sys/time.h> #include <sys/resource.h> int main(int ar ...

  7. Tomcat配置 设置启动参数,点击startup.bat启动

    catalina.batrem ---------------------------------------------------------------------------rem Set J ...

  8. 可变参数宏...和__VA_ARGS__

    __VA_ARGS__ 是一个可变参数的宏,很少人知道这个宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持).实现思想就是宏定义中参数列表的最后一个参数为 ...

  9. 公司名称后缀 Inc. Co.,Ltd.

    Inc. = Incorporated Co.,Ltd."连在一起为Company Limited,就是有限公司,或者有限责任公司.“Co”后面的“.”是英文中表示词语短缩省略的符号,而“C ...

  10. 字符串移位:如“abcdefghi”右移2位后变成“cdefghiab”

    函数头: //pStr 是指向以'\0'结尾的字符串指针 //steps 是要求移动的步数 void LoopMove(char *pStr, int steps) { //...... } 使用库函 ...