//
//  ViewController.m
//  测试uicollectionview
//
// 
//

#import "ViewController.h"

#define screenHeight [[UIScreen mainScreen]bounds].size.height //屏幕高度
#define screenWidth [[UIScreen mainScreen]bounds].size.width  //屏幕宽度
#define collectionCellRow 3 //具体设置几列

@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
{
    UICollectionView *collectionView;
    NSMutableArray *hArr;//记录每个cell的高度
}
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    hArr = [[NSMutableArray alloc]init];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];//设置横向还是竖向
    collectionView  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight) collectionViewLayout:flowLayout];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    [collectionView setBackgroundColor:[UIColor clearColor]];
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    
    collectionView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:collectionView];
    
}

#pragma mark -- UICollectionViewDataSource
//定义展示的uicollectionviewcell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 40;
}
//定义展示的section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


//每个uicollectionview展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"UICollectionViewCell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor  = [UIColor colorWithRed:((100 +indexPath.row) / 255.0) green:((2* indexPath.row)/255.0) blue:((3* indexPath.row)/255.0) alpha:1.0f];;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 20)];
    label.textColor = [UIColor redColor];
    label.text = [NSString stringWithFormat:@"%d",indexPath.row];
    
    //移除cell
    for (id subView  in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    NSInteger remainder = indexPath.row%collectionCellRow;
    
    NSLog(@"remainder is %i ",remainder);//每一列
    NSInteger currentRow = indexPath.row/collectionCellRow;
    NSLog(@"currentrow is %i",currentRow);//每一行,其实是每一行
    CGFloat currentHeight = [hArr[indexPath.row] floatValue];
    
    CGFloat positionX = (screenWidth/collectionCellRow - 8)*remainder + 5*(remainder + 1);
    CGFloat positionY = (currentRow + 1)*5;
    for (NSInteger i = 0; i < currentRow; i++) {
        NSInteger position  =  remainder + i*collectionCellRow;
        positionY += [hArr[position] floatValue];
    }
    cell.frame =  CGRectMake(positionX, positionY, screenWidth/collectionCellRow - 8, currentHeight);
    [cell.contentView addSubview:label];
    return cell;
    
}
#pragma mark --UICollectionViewDelegteFlowLayout
//定义每个Item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 100 + (arc4random()%120);
    //CGFloat height = 100;
    [hArr addObject:[NSString stringWithFormat:@"%f",height]];
    return CGSizeMake(screenWidth/collectionCellRow - 8, height);//设置cell宽高
}

//定义每个UICollectionView的margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(300,100 , 100, 0);
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.backgroundColor = [UIColor greenColor];
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


@end


IOS瀑布流通过UICollectionView控件实现_#define