欢迎光临
我们一直在努力

瀑布流(collectionView)

建站超值云服务器,限时71元/月

瀑布流(collectionView)

项目介绍:

瀑布流(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)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 瀑布流(collectionView)
分享到: 更多 (0)