瀑布流

#import "ViewController.h"

头文件

#import "WaterFallLayout.h"

#import "MyCollectionViewCell.h"

#import "MyImage.h"

#import "UIImageView+WebCache.h"

<UICollectionViewDataSource,UICollectionViewDelegate>

//定义属性网格对象、图片数组

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,strong)NSMutableArray *array;


- (void)viewDidLoad {

[super viewDidLoad];

//初始化图片数组

self.array = [NSMutableArray array];

//请求数据

NSString *path = [[NSBundle mainBundle]pathForResource:@"0" ofType:@"plist"];

NSArray *arr = [NSArray arrayWithContentsOfFile:path];

for (NSDictionary *dic in arr) {

//初始化图片对象

MyImage *mi = [[MyImage alloc]init];

mi.url = dic[@"img"];

mi.width = dic[@"w"];

mi.height = dic[@"h"];

mi.price = dic[@"price"];

//加入数组

[self.array addObject:mi];

}

//初始化布局流对象

WaterFallLayout *layout = [[WaterFallLayout alloc]init];

//设置列数

layout.columnCount = 3;

//设置列间距

layout.columnSpacing = 10;

//设置行间距

layout.rowSpacing = 10;

//设置边距

layout.sectionInsets = UIEdgeInsetsMake(10, 10, 10, 10);

//block返回单元格高度

layout.getItemHeight = ^float(float itemWidth, NSIndexPath *indexPath) {

//获取单元格对应图片对象

MyImage *mi = self.array[indexPath.item];

//单元格高度 = 图片高度 / 图片宽度 * 单元格宽度

return [mi.height floatValue] / [mi.width floatValue] * itemWidth;

};

//初始化网格对象

self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];

//设置代理

self.collectionView.dataSource = self;

self.collectionView.delegate = self;

//背景颜色

self.collectionView.backgroundColor = [UIColor whiteColor];

//注册网格单元格

[self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cellid"];

//加入self视图

[self.view addSubview:self.collectionView];

}

//设置行数

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

return self.array.count;

}

//设置单元格内容

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellid" forIndexPath:indexPath];

//获取图片对象

MyImage *mi = self.array[indexPath.item];

//SDWebImage加载图片

[cell.img sd_setImageWithURL:[NSURL URLWithString:mi.url] placeholderImage:[UIImage imageNamed:@"0.png"]];

cell.img.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height);

//设置价格标签

cell.lab.text = mi.price;

cell.lab.frame = CGRectMake(0, cell.frame.size.height - 30, cell.frame.size.width, 30);

return cell;

}

//点击单元格响应方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

//打印单元格下标

NSLog(@"%ld",indexPath.item);

}


创建WaterFallLayout页面继承UICollectionViewLayout

.h

//定义属性列数、列间距、行间距、边距、单元格高度、列高度数组、单元格属性数组

@property (nonatomic,assign)int columnCount;

@property (nonatomic,assign)float columnSpacing,rowSpacing;

@property (nonatomic,assign)UIEdgeInsets sectionInsets;

@property (nonatomic,strong)float (^getItemHeight)(float itemWidth,NSIndexPath *indexPath);

@property (nonatomic,strong)NSMutableArray *columnHeightArray,*attributesArray;

.m

//懒加载

- (NSMutableArray *)columnHeightArray

{    

if (!_columnHeightArray)

 {       

 _columnHeightArray = [NSMutableArray array];  

  }   

 return _columnHeightArray;

}

- (NSMutableArray *)attributesArray

{    

if (!_attributesArray) 

{       

 _attributesArray = [NSMutableArray array];

    }   

 return _attributesArray;

}

//布局前准备

- (void)prepareLayout

{    //更新数组  

  [self.columnHeightArray removeAllObjects];   

 //初始化列高数组    

for (int i = 0; i < self.columnCount; i ++) 

{        

//初值 = 上边距      

  [self.columnHeightArray addObject:@(self.sectionInsets.top)];  

  }   

 //更新数组  

  [self.attributesArray removeAllObjects];    

//初始化单元格属性数组  

  //获取单元格数量  

  NSInteger count = [self.collectionView numberOfItemsInSection:0];   

 for (int i = 0; i < count; i ++) 

{        

//获取单元格属性     

   UICollectionViewLayoutAttributes *attributes = [self layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];      

  //加入数组       

 [self.attributesArray addObject:attributes];   

 }}

//设置滚动范围

- (CGSize)collectionViewContentSize

{   

 //获取最长的一列高度 

   __block float maxColumnHeight = 0;   

 //遍历数组   

 [self.columnHeightArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)

 {     

   if (maxColumnHeight < [obj floatValue])

 {        

    maxColumnHeight = [obj floatValue];    

    }   

 }];  

  //滚动高度 = 最大列高 + 下边距  

  return CGSizeMake(0, maxColumnHeight + self.sectionInsets.bottom);}

//设置单元格属性

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

{  

  //获取单元格属性    

UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];  

  //获取网格宽度   

 float width = self.collectionView.frame.size.width;  

  //item的宽度 = (collectionView的宽度 - 左边距 - 右边距 - 列距 * (列数 - 1)) / 列数    float itemWidth = (width - self.sectionInsets.left - self.sectionInsets.right - self.columnSpacing * (self.columnCount - 1)) / self.columnCount;  

  //获取最短的一列高度 

   __block float minColumnHeight = MAXFLOAT;   

 //下标 

   __block NSUInteger minColumnIndex = 0;  

  //遍历数组   

 [self.columnHeightArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)

 {     

   if (minColumnHeight > [obj floatValue])

 {         

   minColumnHeight = [obj floatValue];     

       minColumnIndex = idx;     

   }  

  }];   

 //item的x值 = 左边距 + (item宽度 + 列间距) * 最短列下标 

   float itemX = self.sectionInsets.left + (itemWidth + self.columnSpacing) * minColumnIndex;   

 //item的y值 = 最短列的高度 + 行间距   

 float itemY = minColumnHeight + self.rowSpacing; 

   //设置单元格高度(通过block传值获取)  

  float itemHeight = self.getItemHeight(itemWidth,indexPath);  

  //设置单元格位置   

 attributes.frame = CGRectMake(itemX, itemY, itemWidth, itemHeight); 

   //更新列高数组    

self.columnHeightArray[minColumnIndex] = @(itemY + itemHeight); 

   return attributes;

}

//返回所有单元格属性

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect

{

return self.attributesArray;

}


创建MyCollectionViewCell页面继承UICollectionViewCell

.h

//定义属性图片视图、价格标签

@property (nonatomic,strong)UIImageView *img;

@property (nonatomic,strong)UILabel *lab;

.m

//初始化图片视图

- (UIImageView *)img

{

if (!_img) {

_img = [[UIImageView alloc]init];

[self addSubview:_img];

}

return _img;

}

//初始化价格标签

- (UILabel *)lab

{

if (!_lab) {

_lab = [[UILabel alloc]init];

_lab.textAlignment = NSTextAlignmentCenter;

_lab.backgroundColor = [UIColor lightGrayColor];

_lab.alpha = 0.6;

[self addSubview:_lab];

}

return _lab;

}

创建MyImage页面继承NSObject

.h

//定义属性图片地址、图片宽、图片高、价格

@property (nonatomic,strong)NSString *url,*width,*height,*price;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容