好多人都會遇到瀑布流的問題,有些瀑布流的高度是不同的,有些是每一個cell的高度是同樣的,我這裏的寫的只要改動一個地方就能夠了dom
首先自定義繼承佈局
UICollectionViewLayout的類而後再.m文件中佈局屬性atom
/** 默認的列數 */orm
static const NSInteger XMGDefaultColumnCount = 2;繼承
/** 每一列之間的間距 */rem
static const CGFloat XMGDefaultColumnMargin = 10;string
/** 每一行之間的間距 */it
static const CGFloat XMGDefaultRowMargin = 10;io
/** 邊緣間距 */table
static const UIEdgeInsets XMGDefaultEdgeInsets = {0, 0, 0, 0};
在類別中定義這幾個全局變量
/** 存放全部cell的佈局屬性 */
@property (nonatomic, strong) NSMutableArray *attrsArray;
/** 存放全部列的當前高度 */
@property (nonatomic, strong) NSMutableArray *columnHeights;
接下來就是實現了
- (NSMutableArray *)columnHeights
{
if (!_columnHeights) {
_columnHeights = [NSMutableArray array];
}
return _columnHeights;
}
- (NSMutableArray *)attrsArray
{
if (!_attrsArray) {
_attrsArray = [NSMutableArray array];
}
return _attrsArray;
}
/**
* 初始化
*/
- (void)prepareLayout
{
[super prepareLayout];
// 清除之前計算的全部高度
[self.columnHeights removeAllObjects];
for (NSInteger i = 0; i < XMGDefaultColumnCount; i++) {
[self.columnHeights addObject:@(XMGDefaultEdgeInsets.top)];
}
// 清除以前全部的佈局屬性
[self.attrsArray removeAllObjects];
// 開始建立每個cell對應的佈局屬性
NSInteger count = [self.collectionView numberOfItemsInSection:0];
for (NSInteger i = 0; i < count; i++) {
// 建立位置
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
// 獲取indexPath位置cell對應的佈局屬性
UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexPath];
[self.attrsArray addObject:attrs];
}
}
/**
* 決定cell的排布
*/
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
return self.attrsArray;
}
/**
* 返回indexPath位置cell對應的佈局屬性
*/
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
// 建立佈局屬性
UICollectionViewLayoutAttributes *attrs = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
// collectionView的寬度
CGFloat collectionViewW = self.collectionView.frame.size.width;
// 設置佈局屬性的frame
CGFloat w = (collectionViewW - XMGDefaultEdgeInsets.left - XMGDefaultEdgeInsets.right - (XMGDefaultColumnCount - 1) * XMGDefaultColumnMargin) / XMGDefaultColumnCount;
CGFloat h = 50 + arc4random_uniform(100);
// CGFloat h = 40 ;//固定高度爲40
// 找出高度最短的那一列
NSInteger destColumn = 0;
CGFloat minColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 1; i < XMGDefaultColumnCount; i++) {
// 取得第i列的高度
CGFloat columnHeight = [self.columnHeights[i] doubleValue];
if (minColumnHeight > columnHeight) {
minColumnHeight = columnHeight;
destColumn = i;
}
}
CGFloat x = XMGDefaultEdgeInsets.left + destColumn * (w + XMGDefaultColumnMargin);
CGFloat y = minColumnHeight;
if (y != XMGDefaultEdgeInsets.top) {
y += XMGDefaultRowMargin;
}
attrs.frame = CGRectMake(x, y, w, h);
// 更新最短那列的高度
self.columnHeights[destColumn] = @(CGRectGetMaxY(attrs.frame));
return attrs;
}
- (CGSize)collectionViewContentSize
{
CGFloat maxColumnHeight = [self.columnHeights[0] doubleValue];
for (NSInteger i = 1; i < XMGDefaultColumnCount; i++) {
// 取得第i列的高度
CGFloat columnHeight = [self.columnHeights[i] doubleValue];
if (maxColumnHeight < columnHeight) {
maxColumnHeight = columnHeight;
}
}
return CGSizeMake(0, maxColumnHeight + XMGDefaultEdgeInsets.bottom);
}
好的,下面咱們就要在VC中實現
static NSString * const XMGShopId = @"lxtShop";
- (void)viewDidLoad {
[super viewDidLoad];
// 建立佈局
XMGWaterflowLayout *layout = [[XMGWaterflowLayout alloc] init];
// 建立CollectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
collectionView.dataSource = self;
[self.view addSubview:collectionView];
// 註冊
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:XMGShopId];
}
#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 50;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XMGShopId forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];
NSInteger tag = 10;
UILabel *label = (UILabel *)[cell.contentView viewWithTag:tag];
if (label == nil) {
label = [[UILabel alloc] init];
label.tag = tag;
[cell.contentView addSubview:label];
}
label.text = [NSString stringWithFormat:@"%zd", indexPath.item];
[label sizeToFit];
return cell;
}
好了,這樣就能夠實現了。