iOS开发-UICollectionView实现瀑布流
关于瀑布流的实现网上有很多种解法,自定义控件,TableView+ScrollView,UICollectionView是iOS6发布之后用于展示集合视图,算起来已经发布三年左右了,不过知识点是不变的,集合视图提供了一个更优雅的方式去展示图片或者文字信息。UICollectionView与UITableView相似,UICollectionViewController与UITableViewController都负责视图,存储需要的数据,并且能处理数据源与委托协议。
简单瀑布流
首先来看一个简单的UICollectionView实现瀑布流的过程,熟悉之后就可以实现稍微实用的瀑布流:
1.故事板中拖拽一个UICollectionView放在视图中,效果如下:

2.新建一个继承子UICollectionViewCell的子类MyCollectionViewCell,将单元格的Class选定为MyCollectionViewCell,并且设置Identifier为MyCell。
3.实现UICollectionViewDelegate,UICollectionViewDataSource,设置区域,设置区域中的Item个数,生成可复用的单元格:
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 100;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *myCell=[collectionView dequeueReusableCellWithReuseIdentifier:
@"MyCell" forIndexPath:indexPath];
[myCell setBackgroundColor:[UIColor greenColor]];
return myCell;
}
viewDidLoad中设置一下数据源和代理:
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
self.collectionView.backgroundColor=[UIColor whiteColor];
最终效果如下:

石工布局(瀑布流)
上面那种效果其实还是比较简单的,很多情况下所有的单元格的宽度是一定的,只是高度不确定,这就是有些人说的定宽不定高,主要是从视觉上的美感来看,当然我们可以通过实现UICollectionViewDelegateFlowLayout去改变单元格大小:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat height=100+(arc4random()%100);
return CGSizeMake(100, height);
}
通过效果我们可以发现,同一行的单元格的圆心所在的Y轴坐标都是一样的:

石工布局(masonry layout)最早是Pinterest使用,石工简单理解就是前面贴砖的时候每块砖肯能是长方形的也可能正方形的,最终贴出来的效果是长方形或者正方形,国内的话基本上就称为瀑布流,早先是Web网站上流行,现在在手机上看,有些Web甚至整个网站的内容都在一个瀑布流页面,也有点审美疲劳的感觉。
这次先看下最终实现的效果:

这个应该算是最常见的布局模式了,这个的实现很简单,为了Demo的效果没有没有采取任何优化措施,仅供入门参考,设置存储所有高度的数组:
//存储所有的高度的数组
@property (strong,nonatomic) NSMutableArray *heightArr;
将高度添加到数组中:
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
CGFloat height=100+(arc4random()%160);
[self.heightArr addObject:[NSString stringWithFormat:@"%f",height]];
return CGSizeMake(100, height);
}
修改每一行单元格的位置:
// [myCell setBackgroundColor:[UIColor greenColor]];
// return myCell;
//} -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
MyCollectionViewCell *myCell=[collectionView dequeueReusableCellWithReuseIdentifier:
@"MyCell" forIndexPath:indexPath];
[myCell setBackgroundColor:[UIColor redColor]];
NSInteger remainder=indexPath.row%3;
NSInteger currentRow=indexPath.row/3;
CGFloat currentHeight=[self.heightArr[indexPath.row] floatValue]; CGFloat positonX=100*remainder+10*(remainder+1);
CGFloat positionY=(currentRow+1)*10;
for (NSInteger i=0; i<currentRow; i++) {
NSInteger position=remainder+i*3;
positionY+=[self.heightArr[position] floatValue];
}
myCell.frame = CGRectMake(positonX, positionY,100,currentHeight) ; NSUInteger *randomNumber=arc4random_uniform(9);
NSString *girlFilename = [NSString stringWithFormat:@"Girl%lu.jpg", (unsigned long)randomNumber];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:girlFilename]]; [myCell setBackgroundView:imageView];
return myCell;
}
效果演示:

iOS开发-UICollectionView实现瀑布流的更多相关文章
- iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)
一个瀑布流的实现有三种方式: 继承自UIScrollView,仿写UITableView的dataSource和delegate,创造一个缓存池用来实现循环利用cell 写多个UITableview( ...
- 【iOS开发】collectionView 瀑布流实现
一.效果展示 二.思路分析 1> 布局的基本流程 当设置好collectionView的布局方式之后(UICollectionViewFlowLayout),当系统开始布局的时候,会调用 pre ...
- ios图文混编瀑布流
ios图文混编瀑布流,利用UICollectionView 实现图文混编的瀑布流,支持section内容伸缩 http://www.huiyi8.com/pubuliu/
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
- iOS自定义UICollectionViewLayout之瀑布流
目标效果 因为系统给我们提供的 UICollectionViewFlowLayout 布局类不能实现瀑布流的效果,如果我们想实现 瀑布流 的效果,需要自定义一个 UICollectionViewLay ...
- 利用UICollectionView实现瀑布流
利用UICollectionView实现瀑布流通过自定义布局来实现. - 自定义类继承UICollectionViewLayout: 必须重写的方法有: //决定每个item的位置: - (nulla ...
- iOS开发- UICollectionView详解+实例
本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...
- [转] iOS开发- UICollectionView详解+实例
本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...
- iOS开发:AVPlayer实现流音频边播边存
1. AVPlayer简介 AVPlayer存在于AVFoundation中,可以播放视频和音频,可以理解为一个随身听 AVPlayer的关联类: AVAsset:一个抽象类,不能直接使用,代表一个要 ...
随机推荐
- 使用 Python 在 Linux 上实现一键回归测试
从代码库迁出代码 —- pexpect 的使用 测试人员从代码库(例如 CVS )迁出代码的过程中,需要手动输入访问密码,而 Python 提供了 Pexpect 模块则能够将手动输入密码这一过程自动 ...
- [HDU5714]拍照
[HDU5714]拍照 题目大意: 河上有\(n(n\le10^4)\)个船只,小明希望把尽可能多的船只都完整地拍到一张照片中. 小明位于河的边上,并且可以在河边的任意位置进行拍照,照相机的视野恰好为 ...
- JDK 动态代理的简单理解
动态代理 代理模式是 Java 中的常用设计模式,代理类通过调用被代理类的相关方法,提供预处理.过滤.事后处理等服务,动态代理及通过反射机制动态实现代理机制.JDK 中的 java.lang.refl ...
- CentOS下使用LVM进行分区(转)
说明:为什么抄,因为这篇文章图文并茂,所有测试都在CentOS 6和7测试过. 许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统 ...
- 用.Net如何访问Linux下目录
很多Windows下的应用需要访问和监控Linux下的目录,本文便介绍如何实现. 只需要搭建配置samba服务,即可将Linux下的目录变得如同Windows下共享可写. 1.服务查询 默认情况下,L ...
- 自定义两行可左右滑动的GridView
效果图: xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:an ...
- java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.w ...
- .NET:分布式事务
背景 分布式事务使用起来比较方便,不过也是有成本的,因此如果可以不用就尽量不用,比如:采用saga.如果采用了分布式事务的话,就需要对分布式事务相关的几个概念有所了解. 分布式事务 相关角色: 事务发 ...
- Android 4.4 Kitkat Phone工作流程浅析(八)__Phone状态分析
本文来自http://blog.csdn.net/yihongyuelan 转载请务必注明出处 本文代码以MTK平台Android 4.4为分析对象.与Google原生AOSP有些许差异.请读者知悉. ...
- Java删除List和Set集合中元素
今天在做项目时,需要删除List和Set中的某些元素,当时使用边遍历,边删除的方法,却报了以下异常: ConcurrentModificationException 为了以后不忘记,使用烂笔头把它记录 ...