您的位置:首页 > 产品设计 > UI/UE

第三方瀑布流layout:UICollectionViewWaterfallLayout

2016-02-29 21:03 507 查看
1.导入第三方类UICollectionViewWaterfallLayout头文件
2.先创建一个布局layout,再创建一个collectionView放在layout上.
3.调用collection的代理方法设置单元格视图和个数
4.调用layout的代理方法设置单元格的高度


Demo

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor whiteColor];

UICollectionViewWaterfallLayout *layout = [[UICollectionViewWaterfallLayout alloc]init];
layout.itemWidth = (320-20-10-10)/3.0;
layout.columnCount = 5;
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.delegate = self;

UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:collectionView];

[collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:@"cell"];

_totalArr = [[NSMutableArray alloc]init];

//调用自定义的请求方法
[MyRequest productMessgesWithSuccess:^(NSDictionary *dic) {

//        NSLog(@"dic >>>>%@",dic);

//dic 是最外层的字典

NSArray *arr = [[dic objectForKey:@"data"] objectForKey:@"items"];
for (NSDictionary *aDic in arr) {
Model *model = [[Model alloc]initWithDic:aDic];

[_totalArr addObject:model];

}

//获取数据源之后加载UI界面
[collectionView reloadData];

} faile:^(NSError *error) {
NSLog(@"error>>>>>>%@",error);
}];

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

return _totalArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor orangeColor];

Model *model = [_totalArr objectAtIndex:indexPath.row];

float height = [model.height floatValue]*150/[model.width floatValue];

cell.btn.frame = CGRectMake(0, 0, (320-20-10)/2.0, height);
[cell.btn setBackgroundImageForState:UIControlStateNormal withURL:[NSURL URLWithString:model.picUrl]];

cell.lab.frame = CGRectMake(0, height, (320-20-10)/2.0, 44);
cell.lab.text = model.miaoShu;

return cell;

}

//自定义layout的代理方法
- (CGFloat)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewWaterfallLayout *)collectionViewLayout
heightForItemAtIndexPath:(NSIndexPath *)indexPath{

Model *model = [_totalArr objectAtIndex:indexPath.row];

float height = [model.height floatValue]*150/[model.width floatValue];

return height + 44;//44 label 高度

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: