集合视图的瀑布流

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

//设置存储所有cell的高度的数组
@property(nonatomic,strong) NSMutableArray *heightsArray;


@end

@implementation ViewController

static NSString *const cellIdentify = @"cellID";

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //创建集合视图布局对象
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    //设置每一行cell之间的最小间距
//    flowLayout.minimumInteritemSpacing = 10;
   // flowLayout.minimumLineSpacing = 50;
    
    
    //创建集合视图
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.collectionView];
    
    //设置代理和数据源
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    //注册cell
    [self.collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:cellIdentify];

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 30;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellIdentify forIndexPath:indexPath];
    
      cell.imgView.image = [UIImage imageNamed:@"beau.jpg"];
    
    
    //移除cell中的所有子视图
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    
    //    获取此时的cell所在的列(每行3个cell)
    NSInteger col = indexPath.row % 3;
    
    
    //获取此时的cell所在的行
    NSInteger row = indexPath.row / 3;
   
    
    //设置此时的cell的x坐标
    CGFloat positionX = (col + 1) * 20 + 100 * col;
    
    
    //设置此时的cell的Y坐标所在的空隙
    CGFloat positionY = (row+1)*10;
    
   
    //获得此时cell的高度
    CGFloat currentHeight = [self.heightsArray[indexPath.item] floatValue];
    
    //NSLog(@"%f",currentHeight);
    
    //循环每一行,设置此时cell的Y坐标
    for (int i = 0; i < row ;i++) {
        NSInteger position = i*3+col;
        positionY += [self.heightsArray[position] floatValue];
        
    }
//
    //第二种方法:
//        NSString *heightStr = self.heightsArray[indexPath.item];
//        CGRect frame  = cell.frame;
//        frame.size.height = heightStr.floatValue;
//        cell.frame = frame;
//    //cell.backgroundColor = [UIColor redColor];
//    cell.imgView.image = [UIImage imageNamed:@"beau.jpg"];
        //NSLog(@"%@", NSStringFromCGRect(cell.frame));
    
    //    //设置cell的位置和大小
    cell.frame = CGRectMake(positionX, positionY, 100, currentHeight);
//
 
//    重新添加视图
    [cell.contentView addSubview:cell.imgView];
   
    return cell;
    
}



//设置每个cell的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    //随机产生cell的高度
    CGFloat height = 100 + arc4random()%60;
    
    //    将浮点型的高度转换成字符串对象
    NSString *heightString = [NSString stringWithFormat:@"%f",height];
    
    //存储此时cell的高度
    [self.heightsArray addObject:heightString];
    
 
    
    return CGSizeMake(100, height);
    

}



//懒加载
-(NSMutableArray *)heightsArray
{
    if (_heightsArray == nil) {
        _heightsArray = [NSMutableArray array];
    }
    return _heightsArray;
}
@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值