一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - IOS - IOS簡單實現(xiàn)瀑布流UICollectionView

IOS簡單實現(xiàn)瀑布流UICollectionView

2021-01-05 12:23tanhui_ui IOS

這篇文章主要為大家介紹了IOS簡單實現(xiàn)瀑布流UICollectionView的相關(guān)資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

uicollectionview 比tableview 靈活,功能也強大很多。系統(tǒng)實現(xiàn)了流式布局,但用處還有很多限制。

要想實現(xiàn)更靈活的布局,就咬重寫uicollectionviewlayout。
先看下實現(xiàn)效果:

IOS簡單實現(xiàn)瀑布流UICollectionView

廢話不多說,直接上代碼:

先看waterfallcollectionlayout.m

?
1
2
3
4
5
6
7
8
9
10
#import "waterfallcollectionlayout.h"
#define colmargin 5
#define colcount 4
#define rolmargin 5
@interface waterfallcollectionlayout ()
//數(shù)組存放每列的總高度
@property(nonatomic,strong)nsmutablearray* colsheight;
//單元格寬度
@property(nonatomic,assign)cgfloat colwidth;
@end

該類要重寫以下方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
//完成布局前的初始工作
-(void)preparelayout;
 
//collectionview的內(nèi)容尺寸
-(cgsize)collectionviewcontentsize;
 
//為每個item設(shè)置屬性
-(uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath;
 
//獲取制定范圍的所有item的屬性
-(nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect;
 
-(bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds;

每次調(diào)用會清空colsheight數(shù)組里的信息:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//完成布局前的初始工作
-(void)preparelayout{
  [super preparelayout];
  self.colwidth =( self.collectionview.frame.size.width - (colcount+1)*colmargin )/colcount;
  //讓它重新加載
  self.colsheight = nil;
}
通過遍歷colheight數(shù)組里的所有列來獲得最長的那一列,返回contentsize
//collectionview的內(nèi)容尺寸
-(cgsize)collectionviewcontentsize{
  nsnumber * longest = self.colsheight[0];
  for (nsinteger i =0;i<self.colsheight.count;i++) {
    nsnumber* rolheight = self.colsheight[i];
    if(longest.floatvalue<rolheight.floatvalue){
      longest = rolheight;
    }
  }
  return cgsizemake(self.collectionview.frame.size.width, longest.floatvalue);
}

每個cell要出來時這個方法會被調(diào)用,在此方法中設(shè)置該cell的frame。

注意heightblock是外部控制器傳進來的block用以計算每個cell的高度,現(xiàn)在我只是設(shè)置了隨機數(shù)。如果沒有傳block進來我這里直接讓他崩潰了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//為每個item設(shè)置屬性
-(uicollectionviewlayoutattributes *)layoutattributesforitematindexpath:(nsindexpath *)indexpath{
  uicollectionviewlayoutattributes* attr = [uicollectionviewlayoutattributes layoutattributesforcellwithindexpath:indexpath];
  nsnumber * shortest = self.colsheight[0];
  nsinteger shortcol = 0;
  for (nsinteger i =0;i<self.colsheight.count;i++) {
    nsnumber* rolheight = self.colsheight[i];
    if(shortest.floatvalue>rolheight.floatvalue){
      shortest = rolheight;
      shortcol=i;
    }
  }
  cgfloat x = (shortcol+1)*colmargin+ shortcol * self.colwidth;
  cgfloat y = shortest.floatvalue+colmargin;
  
  //獲取cell高度
  cgfloat height=0;
  nsassert(self.heightblock!=nil, @"未實現(xiàn)計算高度的block ");
  if(self.heightblock){
    height = self.heightblock(indexpath);
  }
  attr.frame= cgrectmake(x, y, self.colwidth, height);
  self.colsheight[shortcol]=@(shortest.floatvalue+colmargin+height);
  
  return attr;
}
?
1
2
3
4
5
6
7
8
9
10
//獲取所有item的屬性
-(nsarray<uicollectionviewlayoutattributes *> *)layoutattributesforelementsinrect:(cgrect)rect{
  nsmutablearray* array = [nsmutablearray array];
  nsinteger items = [self.collectionview numberofitemsinsection:0];
  for (int i = 0; i<items;i++) {
    uicollectionviewlayoutattributes* attr = [self layoutattributesforitematindexpath:[nsindexpath indexpathforitem:i insection:0]];
    [array addobject:attr];
  }
  return array;
}

實現(xiàn)下列方法會在出現(xiàn)新的cell時重新布局并調(diào)用preparelayout方法

?
1
2
3
-(bool)shouldinvalidatelayoutforboundschange:(cgrect)newbounds{
  return yes;
}

每列高度的存放,初始高度可以改,我這里是0

?
1
2
3
4
5
6
7
8
9
10
11
-(nsmutablearray *)colsheight{
  if(!_colsheight){
    nsmutablearray * array = [nsmutablearray array];
    for(int i =0;i<colcount;i++){
      //這里可以設(shè)置初始高度
      [array addobject:@(0)];
    }
    _colsheight = [array mutablecopy];
  }
  return _colsheight;
}

再來看看控制器里就是這么簡單

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma mark getter-setter
-(uicollectionview *)collectionview{
  if(!_collectionview){
    _collectionview = [[uicollectionview alloc]initwithframe:self.view.frame collectionviewlayout:self.layout];
    _collectionview.backgroundcolor = [uicolor whitecolor];
    _collectionview.delegate=self;
    _collectionview.datasource=self;
    [_collectionview registerclass:[collectionviewcell class] forcellwithreuseidentifier:identifer];
  }
  return _collectionview;
}
-(uicollectionviewlayout *)layout{
  if(!_layout){
    _layout = [[waterfallcollectionlayout alloc]initwithitemsheightblock:^cgfloat(nsindexpath *index) {
      return [self.heightarr[index.item] floatvalue];
    }];
    
  }
  return _layout;
}
-(nsarray *)heightarr{
  if(!_heightarr){
    //隨機生成高度
    nsmutablearray *arr = [nsmutablearray array];
    for (int i = 0; i<100; i++) {
      [arr addobject:@(arc4random()%50+80)];
    }
    _heightarr = [arr copy];
  }
  return _heightarr;
}

關(guān)于瀑布流的文章特別多,本文就是為大家分享了ios簡單實現(xiàn)瀑布流的方法,希望對大家的學(xué)習(xí)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 深夜福利免费在线观看 | 女人张开腿让男人桶视频免费大全 | 免费视频网 | 亚洲免费精品 | 99热这里只有精品在线观看 | 亚洲国产精品福利片在线观看 | 国产成人综合久久 | 国产午夜精品一区二区 | 男同桌扒开女同桌胸罩喝奶 | 小SAO货边洗澡边CAO你动漫 | 国产偷窥 | 免费毛片 | 韩国帅男同gay网站 韩国三级在线播放 | 欧美日韩亚洲第一区在线 | 特级淫片大乳女子高清视频 | 欧美特黄一级大片 | 夫妻性生活一级黄色片 | www国产精品 | 高h全肉动漫在线观看免费 高h辣h双处全是肉军婚 | 亚洲毛片网 | 亚洲国产精久久久久久久 | 国产成人精品一区二三区2022 | 美女黄金大片视频免费看 | 欧美午夜视频一区二区 | 日本护士xxxx视频 | 日本成人免费在线视频 | 日本 在线观看 | 日韩一级片在线观看 | 亚洲久操 | 99久久精品无码一区二区毛片 | 日本高清免费中文字幕不卡 | 激情图片 激情小说 | free service性v极品 | 国产精品久久久久久久久久久久久久 | 91亚洲在线 | 边摸边吃奶玩乳尖视频 | 极品 女神校花 露脸91 | 99久久6er热免费精品 | 婷婷天天 | 欧美成人免费草草影院视频 | 皇上撞着太子妃的秘密小说 |