利用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. Linux 安装g++

    g++ 它的全名不叫g++而是叫gcc-c++; 所以要安装它就可以用 yum install gcc-c++;

  2. Windows XP密钥(共38枚)

    翱翔博客(http://hi.baidu.com/guoguo6688/home) Windows XP Professional VOL版密钥:=========================== ...

  3. C# WinForm判断Win7下是否是管理员身份运行

    原文:C# WinForm判断Win7下是否是管理员身份运行 如果程序不是以管理员身份运行,操作本地文件会提示:System.UnauthorizedAccessException异常 Vista 和 ...

  4. spring+hibernate删除单条记录的几种方法

    spring+hibernate删除单条记录的几种方法

  5. Jsoup代码解读之二-DOM相关对象

    Jsoup代码解读之二-DOM相关对象   之前在文章中说到,Jsoup使用了一套自己的DOM对象体系,和Java XML API互不兼容.这样做的好处是从XML的API里解脱出来,使得代码精炼了很多 ...

  6. Azure 网站和通配符域

     本文章由Azure 网站团队软件开发工程师Michael Candido 撰写 一些 Web 应用程序需要使用多个子域,在某些情况下还需要动态添加新的子域.例如,一个多租户 Web 应用程序可使 ...

  7. 查GDI对象泄露的利器:GDIView

    查GDI对象泄露的利器:GDIView可以很详细的查到进程的GDI对象的总个数,详细的GDI对象的个数,以及其增减数量.其GDI对象类型也可以很详细的得知,以及其内存地址,句柄.实在是好使! 下载地址 ...

  8. 刷新指定行或区 cell

    //一个section刷新 NSIndexSet *indexSetA = [[NSIndexSet alloc]initWithIndex:3];    //刷新第3段 [tableview rel ...

  9. GridView行编辑、更新、取消、删除事件使用方法

    注意:当启用编辑button时,点击编辑button后会使一整行都切换成文本框.为了是一行中的一部分是文本框,须要把以整行的全部列都转换成模板,然后删掉编辑模板中的代码.这样就能使你想编辑的列转换成文 ...

  10. 对武汉-and-IT软件开发的看法

    本编是一个武汉农村娃子,2015年毕业到现在算上实习 差不多快三年的时间了.在软件行业混的也就一般水平,从开心在学校学习的winform+DBHelper 的开发模式,到现在MVC+EF 的开发模式. ...