利用UICollectionView实现瀑布流
利用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实现瀑布流的更多相关文章
- iOS开发-UICollectionView实现瀑布流
关于瀑布流的实现网上有很多种解法,自定义控件,TableView+ScrollView,UICollectionView是iOS6发布之后用于展示集合视图,算起来已经发布三年左右了,不过知识点是不变的 ...
- iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流
上篇博客的实例是自带的UICollectionViewDelegateFlowLayout布局基础上来做的Demo, 详情请看<iOS开发之窥探UICollectionViewControlle ...
- iOS开发:一个瀑布流的设计与实现(已实现缓存池功能,该功能使得瀑布流cell可以循环利用)
一个瀑布流的实现有三种方式: 继承自UIScrollView,仿写UITableView的dataSource和delegate,创造一个缓存池用来实现循环利用cell 写多个UITableview( ...
- iOS开发之窥探UICollectionViewController(四) --一款功能强大的自定义瀑布流
在上一篇博客中<iOS开发之窥探UICollectionViewController(三) --使用UICollectionView自定义瀑布流>,自定义瀑布流的列数,Cell的外边距,C ...
- OC-UICollectionView实现瀑布流
UICollectionView实现瀑布流 在iOS中可以实现瀑布流的目前已知的有2种方案: 使用UIScrollView自己封装一套,这种方案是应用于iOS6之前的,因为iOS6才出来UIColle ...
- 自定义UICollectionLayout布局 —— UIKit之学习UICollectionView记录一《瀑布流》
一.思路 思路一:比较每一行所有列的cell的高度,从上到下(也就是从第一行开始),从最短的开始计算,(记录下b的高度和索引,从开始计算,依次类推) 思路二:设置上.下.左.右间距和行间距.列间距及列 ...
- IOS 瀑布流UICollectionView实现
IOS 瀑布流UICollectionView实现 在实现瀑布流之前先来看看瀑布流的雏形(此方法的雏形 UICollectionView) 对于UICollectionView我们有几点注意事项 它和 ...
- UICollectionView 很简单的写个瀑布流
你项目中要用到它吗? 可能会在你的项目中用到这玩意,最近也是要用就简单的写了一个 Demo.没多少代码,就不放Git了,下面会详细点的说说代码的,要还有什么问题的小伙伴可以直接Q我,也可以把Demo发 ...
- iOS - UICollectionView 瀑布流 添加表头视图的坑
UICollectionView 瀑布流 添加表头视图的坑 首先是,需求加了个头视图在顶部,在collectionView中的头视图跟TableView的不一样,TableView的表头只要设置tab ...
随机推荐
- C#学习日志 day 5 ------ windows phone 8.1真机调试手机应用
在vs2013中,可以写windows phone 8.1的程序,但是调试时需要用到windows自带的虚拟机hyper-V 正版的系统开启hyper—V的时候不会有问题,但是盗版的系统可能导致系统不 ...
- bzoj 2542: [Ctsc2001]终极情报网 费用流
题目链接 2542: [Ctsc2001]终极情报网 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 321 Solved: 125[Submit][S ...
- zoj 3171 The Hidden 7's
这道题,我在网上看到两种dp,不过基本原理是一样的,不过感觉还是后面的一种比较巧妙!因为我对动态不是很熟,自能加上一些自己的理解,写上注释. 1) #include <stdio.h> # ...
- 认识和理解css布局中的BFC
认识和理解css布局中的BFC BFC的定义 是 W3C CSS 2.1 规范中的一个概念,它决定了元素如何对其内容进行定位,以及与其他元素的关系和相互作用. Block Formatting Con ...
- CNZZ公告:近期无法获取百度关键词
今天登录cnzz网站统计,出现一条公告,说是“关于近期无法获取百度关键词的公告”,内容如下: 近日部分用户反馈百度搜索词流量出现不同程度的下降.经排查,是由于百度搜索引擎调整了URL规则,取消了来源U ...
- 第七届河南省赛H.Rectangles(lis)
10396: H.Rectangles Time Limit: 2 Sec Memory Limit: 128 MB Submit: 229 Solved: 33 [Submit][Status] ...
- JavaScipt实现倒计时方法总结
JavaScript中提供了两种实现计时.延时的方法,分别如下: 一. t = setTimeout(“function()", millisecond) 与 clearTimeout(t) ...
- 浅谈JDBC(二)
JDBC中的事务 简单来说,事务就是要保证一组DAO方法的操作,要么一起成功要么一起失败. 1.事务控制的位置 在Service中的业务方法内进行事务控制. 2.事务控制的代码 a.注意:JDBC会自 ...
- [Jobdu] 题目1283:第一个只出现一次的字符
题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现一次的字符. 输入: 输入有多组数据每一组输入一个字符串. 输出: 输出第一个只出现一次的字 ...
- Hibernate 总结一
Session 当批量处理数据过大时,session这个集合会造成内存溢出,需要通过flush把session中的数据刷出到数据库中,让后再clear,清空缓存 一.集合映射. 类型 Java中声明 ...