3、上边的这个布局类可以直接复制粘贴下来。然后就是创建你的UICollectionView
在collectionView的cell中可以直接创建imageView或者是label添加到cell上,用来显示数据。
collectionView默认section缩进左右是0
调节横向cell间距
layout.minimumLineSpacing = 10;
调节纵向cell间距
layout.minimumInteritemSpacing = 20;
调节瀑布流显示的行数,当然了你的collectionView的高(宽)足够显示几行(列)就会自动显示多上行(列);
layout.numberOfColumns = 3;
#import "RootViewController.h"
#import "WaterfallFlowLayout.h"
@interface RootViewController () <UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,WaterfallFlowLayoutDelegate>
{
UICollectionView * _collectionView;
}
@end
@implementation RootViewController
- (void)dealloc {
[_collectionView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// 创建集合视图
[self createCollectionView];
}
- (UICollectionViewLayout *)createLayout {
#if 1
WaterfallFlowLayout * layout = [[WaterfallFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
layout.minimumLineSpacing = 10;
layout.minimumInteritemSpacing = 20;
layout.numberOfColumns = 3;
layout.delegate = self;
[self performSelector:@selector(changeLayout:) withObject:layout afterDelay:3];
#else
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 10;
layout.itemSize = CGSizeMake(150, 100);
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
#endif
return [layout autorelease];
}
- (void)changeLayout:(WaterfallFlowLayout *)layout {
layout.numberOfColumns = 3;
}
- (void)createCollectionView {
CGRect frame = CGRectMake(0, 20, VIEW_WIDTH, VIEW_HEIGHT-20);
_collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:[self createLayout]];
_collectionView.backgroundColor = [UIColor cyanColor];
// 设置代理
_collectionView.delegate = self;
_collectionView.dataSource = self;
// 注册cell 类型 及 复用标识
[_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellId"];
[self.view addSubview:_collectionView];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 102;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellId" forIndexPath:indexPath];
UILabel * label = nil;
NSArray * array = cell.contentView.subviews;
if (array.count) {
label = array[0];
}else {
label = [[UILabel alloc] init];
// label.frame = cell.bounds;
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont systemFontOfSize:50];
[cell.contentView addSubview:label];
[label release];
}
label.frame = cell.bounds;
label.text = [NSString stringWithFormat:@"%ld",indexPath.item];
label.textColor = [UIColor whiteColor];
cell.backgroundColor = RandomColor;
return cell;
}
- (CGSize) collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake( arc4random()%100+200, 110);
}
-(CGFloat) WaterfallFlowLayout:(WaterfallFlowLayout *)layout widthForItemAtIndexPath:(NSIndexPath *)indexPath{
return arc4random()%150+50;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击了第 %ld 组,第 %ld 行",indexPath.section,indexPath.row);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end










