第三方瀑布流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 高度 }
相关文章推荐
- UITableView的性能优化,让TableView更加的丝滑!
- UIScrollView的常见属性和方法
- CodeForces 450B Jzzhu and Sequences
- bzoj3639: Query on a tree VII
- bzoj3637: Query on a tree VI
- 非UI线程可不可以更新UI(一)
- 作业1-我的第一个博客&GuiHub简单练习
- UIKit框架(17)Quartz2D
- 克服VR眩晕之帧数:提升UE4内容实时渲染效率
- 利用NSValue包装结构体
- UIView中的坐标转换convertPoint:convertRect:
- 解决Carthage 0.12版本之后copy-frameworks build失败
- UIKit框架(16)手势识别器
- Java集合类: Set、List、Map、Queue使用方法详解
- 【慕课笔记】第六章 JAVA中的集合框架(下) 第4节 学生选课——判断Map中是否包含指定的key和value
- 解决键盘弹起时把UINavigationBar顶出屏幕外面的问题
- nyoj119 士兵杀敌(三) (线段树,两个value)
- Implement Queue using Stacks
- IOS开发之UIView总结
- WPF 支持分组互斥的 RadioButton 式单选菜单