利用UICollectionView实现瀑布流通过自定义布局来实现。

- 自定义类继承UICollectionViewLayout;

必须重写的方法有:

//决定每个item的位置;

- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;

//布局layout

- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect;

//滚动的范围,类似于scrollview的contentSize

- (CGSize)collectionViewContentSize;

@protocol WJLWaterFallLayoutDelegate;

@interface WJLWaterFallLayout : UICollectionViewLayout

@property(nonatomic,assign)int colCount;//列数
@property(nonatomic,assign)CGFloat colSpace;//列间距
@property(nonatomic,assign)CGFloat lineSpace;//行间距
@property(nonatomic,assign)UIEdgeInsets sectionInsets;
@property(nonatomic,weak) id <WJLWaterFallLayoutDelegate> delegate; @end @protocol WJLWaterFallLayoutDelegate <NSObject> @required
//通过代理得到每个item 的一个高度
- (CGFloat)collectionViewLayout:(WJLWaterFallLayout*)layout heightAtIndexPath:(NSIndexPath*)indexPath;
#import "WJLWaterFallLayout.h"

static const int colCount = ;//默认列数
static const CGFloat space = ;//默认间距 @interface WJLWaterFallLayout() @property(nonatomic,strong)NSMutableDictionary *maxDic;//储存列的长度 @end @implementation WJLWaterFallLayout - (instancetype)init{
if (self = [super init]) {
self.colCount = colCount;
self.lineSpace = space;
self.colSpace = space;
self.sectionInsets = UIEdgeInsetsMake(space, space, space, space);
self.maxDic = [NSMutableDictionary dictionary];
}
return self;
} - (void)prepareLayout{
[super prepareLayout];
}
/**
* 决定collectionview能滑动的范围
*/
- (CGSize)collectionViewContentSize{
__block NSString *maxCol = @"";
//找到最长的列
[self.maxDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj floatValue] > [self.maxDic[maxCol] floatValue]) {
maxCol = key;
}
}];
CGFloat maxY = [self.maxDic[maxCol] floatValue]+self.lineSpace;
return CGSizeMake(, maxY) ;
}
/**
* 布局layout
*/
- (nullable NSArray<__kindof UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
for (int i = ; i < self.colCount; i ++) {
NSString *key = [NSString stringWithFormat:@"%d",i];
self.maxDic[key] = @;
} NSMutableArray *arr = [NSMutableArray array];
int count = (int)[self.collectionView numberOfItemsInSection:]; for (int i = ; i < count ; i ++) {
UICollectionViewLayoutAttributes *att = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:]];
[arr addObject:att];
} return arr;
}
/**
决定了每个item的位置,大小
*/
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
__block NSString *minCol = @"";
[self.maxDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
if ([obj floatValue] < [self.maxDic[minCol] floatValue] ) {
minCol = key;
}
}];
int col = [minCol intValue];
CGFloat nowY = [self.maxDic[minCol] floatValue];//当前列的y值 CGFloat itemW = (self.collectionView.frame.size.width - self.sectionInsets.left - self.sectionInsets.right - self.colSpace*(self.colCount - ))/(self.colCount);
CGFloat itemH = ;
if ([self.delegate respondsToSelector:@selector(collectionViewLayout:heightAtIndexPath:)]) {
itemH = [self.delegate collectionViewLayout:self heightAtIndexPath:indexPath];
}
CGFloat x = self.sectionInsets.left + (itemW + self.colSpace) * col;
CGFloat space = ; if (indexPath.row < self.colCount) {//第一行的时候y值
space = self.sectionInsets.top;
}else {
space = self.lineSpace;
}
CGFloat y = nowY + space; self.maxDic[minCol] = @(y + itemH);//更新此列的y值 UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
attributes.frame = CGRectMake(x, y, itemW, itemH); return attributes;
}
@end

利用UICollectionView实现瀑布流的更多相关文章

  1. iOS开发-UICollectionView实现瀑布流

    关于瀑布流的实现网上有很多种解法,自定义控件,TableView+ScrollView,UICollectionView是iOS6发布之后用于展示集合视图,算起来已经发布三年左右了,不过知识点是不变的 ...

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

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

  3. iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)

    一个瀑布流的实现有三种方式: 继承自UIScrollView,仿写UITableView的dataSource和delegate,创造一个缓存池用来实现循环利用cell 写多个UITableview( ...

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

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

  5. OC-UICollectionView实现瀑布流

    UICollectionView实现瀑布流 在iOS中可以实现瀑布流的目前已知的有2种方案: 使用UIScrollView自己封装一套,这种方案是应用于iOS6之前的,因为iOS6才出来UIColle ...

  6. 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》

    一.思路 思路一:比较每一行所有列的cell的高度,从上到下(也就是从第一行开始),从最短的开始计算,(记录下b的高度和索引,从开始计算,依次类推) 思路二:设置上.下.左.右间距和行间距.列间距及列 ...

  7. IOS 瀑布流UICollectionView实现

    IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...

  8. UICollectionView 很简单的写个瀑布流

    你项目中要用到它吗? 可能会在你的项目中用到这玩意,最近也是要用就简单的写了一个 Demo.没多少代码,就不放Git了,下面会详细点的说说代码的,要还有什么问题的小伙伴可以直接Q我,也可以把Demo发 ...

  9. iOS - UICollectionView 瀑布流 添加表头视图的坑

    UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...

随机推荐

  1. 星际SC 地图 Big Game Fort 要塞之战 修正了 BIG GAME 地图的平衡性

    星际SC 地图 Big Game Fort 要塞之战 修正了 BIG GAME 地图的平衡性 也适合BIG 1V1 对战 此版本目前不开放1打1造功能

  2. JQ插件开发方法

    由于项目原因,不得不写个JQ侧滑插件来满足需求.. 先引用两篇博文,待测试了 再写怎么做.. http://blog.csdn.net/business122/article/details/8278 ...

  3. Mybatis的ResultMap的使用(转)

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  4. mysql 的卸载、再安装与常用命令

    1.卸载mysql.完全删除: 删除 mysqlsudo apt-get autoremove --purge mysql-server-5.0sudo apt-get remove mysql-se ...

  5. php抽象类和接口

    接口 使用接口,你可以指定某个类必须实现那些方法,但是不需要定义这些方法的具体内容,我们可以通过interface来定义一个接口,就像定义标准类一样,但其中定义所有的方法都是空的,接口中定义的所有的方 ...

  6. Summer Holiday(强联通入度最小点)

    Summer Holiday Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  7. hdu2554-N对数的排列问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2554 假设所有的2n个数据的位置分别从1~2n标号. 现在假设其中第ai个数据(双胞胎),和bi.那么他们的位置 ...

  8. 当前项目与当前环境的JDK版本不匹配”Bad version number in .class file“

    java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLoader.d ...

  9. HDU OJ 5441 Travel 2015online E

    题目:click here 题意: 有个很暴躁的人,想坐车旅行n个城市.连接城市共有m条路(双向).他坐在车上很不爽,每次最多忍耐x分钟.但是每站下车他又可以休息(重新计时).总共有q次询问.问途中有 ...

  10. 解决MySQL 一闪而过的情况

       首先进入cmd 切入MySQL的安装目录,然后切入 bin 目录 ,输入mysqld -n t --skip-grant-tables命令. 这个 cmd 窗口先不要关闭, 打开另一个窗口 登陆 ...