项目介绍:
瀑布流(collectionView), WaterViewController主要设置代码如下:
[Objective-C] 查看源文件 复制代码
#import "ZYWaterViewController.h"
#import "ZYWaterLayout.h"
@interface ZYWaterViewController ()<UICollectionViewDataSource , UICollectionViewDelegate , ZYWaterLayoutDelegate>
@end
@implementation ZYWaterViewController
static NSString *cellID = @"cell";
- (void)viewDidLoad {
[super viewDidLoad];
ZYWaterLayout *layout = [[ZYWaterLayout alloc]init];
layout.delegate = self;
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
collectionView.dataSource = self;
collectionView.delegate = self;
collectionView.showsVerticalScrollIndicator = NO;
collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:collectionView];
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellID];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 120;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
cell.backgroundColor = [self randomColor];
return cell;
}
- (UIColor *)randomColor {
CGFloat red = arc4random_uniform(255);
CGFloat green = arc4random_uniform(255);
CGFloat blue = arc4random_uniform(255);
UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
return color;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"点击了第%ld个item" , (long)indexPath.row);
}
#pragma mark -- ZYWaterLayoutDelegate
//设置每一行的高度
- (CGFloat)waterLayout:(UICollectionViewLayout *)waterLayout itemWidth:(CGFloat)itemWidte indexPath:(NSIndexPath *)indexPath {
return 80 + arc4random_uniform(100);
}
//设置列数
- (NSInteger)columnCountInWaterFlowLayout:(UICollectionViewLayout *)layout {
return 3;
}
//设置边缘间的距离
- (UIEdgeInsets)edgeInsetsInWaterFlowLayout:(UICollectionViewLayout *)layout {
return UIEdgeInsetsMake(20, 10, 10, 10);
}
//设置列间距
- (CGFloat)columnIntervalInWaterFlowLayout:(UICollectionViewLayout *)layout {
return 15;
}
//设置行间距
- (CGFloat)rowIntervalInWaterFlowLayout:(UICollectionViewLayout *)layout {
return 20;
}
@end

可以自动调整列数的CollectionView.zip
(54.33 KB, 下载次数: 231, 售价: 3 金钱)
2016-7-11 11:55 上传
点击文件名下载附件
瀑布流(collectionView)
