@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
//设置存储所有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