瀑布流

#import "ViewController.h"

頭文件

#import "WaterFallLayout.h"

#import "MyCollectionViewCell.h"

#import "MyImage.h"

#import "UIImageView+WebCache.h"

<UICollectionViewDataSource,UICollectionViewDelegate>

//定義屬性網(wǎng)格對(duì)象、圖片數(shù)組

@property (nonatomic,strong)UICollectionView *collectionView;

@property (nonatomic,strong)NSMutableArray *array;


- (void)viewDidLoad {

[super viewDidLoad];

//初始化圖片數(shù)組

self.array = [NSMutableArray array];

//請(qǐng)求數(shù)據(jù)

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

NSArray *arr = [NSArray arrayWithContentsOfFile:path];

for (NSDictionary *dic in arr) {

//初始化圖片對(duì)象

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

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

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

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

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

//加入數(shù)組

[self.array addObject:mi];

}

//初始化布局流對(duì)象

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

//設(shè)置列數(shù)

layout.columnCount = 3;

//設(shè)置列間距

layout.columnSpacing = 10;

//設(shè)置行間距

layout.rowSpacing = 10;

//設(shè)置邊距

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

//block返回單元格高度

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

//獲取單元格對(duì)應(yīng)圖片對(duì)象

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

//單元格高度 = 圖片高度 / 圖片寬度 * 單元格寬度

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

};

//初始化網(wǎng)格對(duì)象

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

//設(shè)置代理

self.collectionView.dataSource = self;

self.collectionView.delegate = self;

//背景顏色

self.collectionView.backgroundColor = [UIColor whiteColor];

//注冊(cè)網(wǎng)格單元格

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

//加入self視圖

[self.view addSubview:self.collectionView];

}

//設(shè)置行數(shù)

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

{

return self.array.count;

}

//設(shè)置單元格內(nèi)容

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

{

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

//獲取圖片對(duì)象

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);

//設(shè)置價(jià)格標(biāo)簽

cell.lab.text = mi.price;

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

return cell;

}

//點(diǎn)擊單元格響應(yīng)方法

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

{

//打印單元格下標(biāo)

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

}


創(chuàng)建WaterFallLayout頁面繼承UICollectionViewLayout

.h

//定義屬性列數(shù)、列間距、行間距、邊距、單元格高度、列高度數(shù)組、單元格屬性數(shù)組

@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;

}

//布局前準(zhǔn)備

- (void)prepareLayout

{? ? //更新數(shù)組??

? [self.columnHeightArray removeAllObjects];? ?

?//初始化列高數(shù)組? ??

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

{? ? ? ??

//初值 = 上邊距? ? ??

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

? }? ?

?//更新數(shù)組??

? [self.attributesArray removeAllObjects];? ??

//初始化單元格屬性數(shù)組??

? //獲取單元格數(shù)量??

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

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

{? ? ? ??

//獲取單元格屬性? ? ?

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

? //加入數(shù)組? ? ? ?

?[self.attributesArray addObject:attributes];? ?

?}}

//設(shè)置滾動(dòng)范圍

- (CGSize)collectionViewContentSize

{? ?

?//獲取最長的一列高度?

?? __block float maxColumnHeight = 0;? ?

?//遍歷數(shù)組? ?

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

?{? ? ?

?? if (maxColumnHeight < [obj floatValue])

?{? ? ? ??

? ? maxColumnHeight = [obj floatValue];? ??

? ? }? ?

?}];??

? //滾動(dòng)高度 = 最大列高 + 下邊距??

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

//設(shè)置單元格屬性

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

{??

? //獲取單元格屬性? ??

UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];??

? //獲取網(wǎng)格寬度? ?

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

? //item的寬度 = (collectionView的寬度 - 左邊距 - 右邊距 - 列距 * (列數(shù) - 1)) / 列數(shù)? ? float itemWidth = (width - self.sectionInsets.left - self.sectionInsets.right - self.columnSpacing * (self.columnCount - 1)) / self.columnCount;??

? //獲取最短的一列高度?

?? __block float minColumnHeight = MAXFLOAT;? ?

?//下標(biāo)?

?? __block NSUInteger minColumnIndex = 0;??

? //遍歷數(shù)組? ?

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

?{? ? ?

?? if (minColumnHeight > [obj floatValue])

?{? ? ? ? ?

?? minColumnHeight = [obj floatValue];? ? ?

?? ? ? minColumnIndex = idx;? ? ?

?? }??

? }];? ?

?//item的x值 = 左邊距 + (item寬度 + 列間距) * 最短列下標(biāo)?

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

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

?float itemY = minColumnHeight + self.rowSpacing;?

?? //設(shè)置單元格高度(通過block傳值獲取)??

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

? //設(shè)置單元格位置? ?

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

?? //更新列高數(shù)組? ??

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

?? return attributes;

}

//返回所有單元格屬性

- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect

{

return self.attributesArray;

}


創(chuàng)建MyCollectionViewCell頁面繼承UICollectionViewCell

.h

//定義屬性圖片視圖、價(jià)格標(biāo)簽

@property (nonatomic,strong)UIImageView *img;

@property (nonatomic,strong)UILabel *lab;

.m

//初始化圖片視圖

- (UIImageView *)img

{

if (!_img) {

_img = [[UIImageView alloc]init];

[self addSubview:_img];

}

return _img;

}

//初始化價(jià)格標(biāo)簽

- (UILabel *)lab

{

if (!_lab) {

_lab = [[UILabel alloc]init];

_lab.textAlignment = NSTextAlignmentCenter;

_lab.backgroundColor = [UIColor lightGrayColor];

_lab.alpha = 0.6;

[self addSubview:_lab];

}

return _lab;

}

創(chuàng)建MyImage頁面繼承NSObject

.h

//定義屬性圖片地址、圖片寬、圖片高、價(jià)格

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,501評(píng)論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,673評(píng)論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,610評(píng)論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,939評(píng)論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,668評(píng)論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,004評(píng)論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,001評(píng)論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,173評(píng)論 0 290
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,705評(píng)論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,426評(píng)論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,656評(píng)論 1 374
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,139評(píng)論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,833評(píng)論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,247評(píng)論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,580評(píng)論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 52,371評(píng)論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,621評(píng)論 2 380

推薦閱讀更多精彩內(nèi)容