UICollectionView瀑布流的实现,带例子
来源:互联网 发布:飞飞cms安装ck播放器 编辑:程序博客网 时间:2024/05/22 11:50
1、实现UICollectionView最重要的就是计算UICollectionViewFlowLayout,因为UICollectionView的每个cell的位置都是根据这个layout决定的
下面贴上主要代码:
#import "ViewController.h"#import "PicModel.h"#import "PicFlowLayout.h"#import "PicCollectionViewCell.h"@interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegate>{ NSMutableArray * _dataArray; UICollectionView * _collectionView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width; _dataArray = [[NSMutableArray alloc] init]; for (int i = 1; i <= 28; i++) { NSString * picPathStr = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"pic%i", i] ofType:@"jpg"]; NSData * picData = [NSData dataWithContentsOfFile:picPathStr]; UIImage * image = [UIImage imageWithData:picData]; PicModel * model = [[PicModel alloc] init]; model.image = image; model.imageHeight = image.size.height; model.imageWidth = image.size.width; model.width = screenWidth/2; model.height = model.imageHeight * (model.width/model.imageWidth); [_dataArray addObject:model]; } PicFlowLayout * layout = [[PicFlowLayout alloc] init]; layout.dataArray = _dataArray; //collectionView的每个cell根据对应的layout进行布局 _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout]; _collectionView.dataSource = self; _collectionView.delegate = self; [self.view addSubview:_collectionView]; [_collectionView registerClass:[PicCollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; _collectionView.backgroundColor = [UIColor whiteColor]; }#pragma mark --#pragma mark UICollectionViewDataSource//返回每个section的cell个数- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _dataArray.count;}//返回cell- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ PicCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; PicModel * model = _dataArray[indexPath.row]; [cell setModel:model]; return cell;}
//// PicFlowLayout.m// 001-uicollectionview//// Created by zhonghj on 16/1/18.// Copyright © 2016年 dzkj. All rights reserved.//#import "PicFlowLayout.h"@implementation PicFlowLayout//指定当前的collectionView的contentSize- (CGSize)collectionViewContentSize{ CGFloat leftHeight = 0.0f;//左边的总高度 CGFloat rightHeight = 0.0f;//右边的总高度 for (int i = 0; i < _dataArray.count; i++) { PicModel * model = _dataArray[i]; if (leftHeight <= rightHeight) { //放左侧 leftHeight += model.height; }else{ rightHeight += model.height; } } return CGSizeMake(0, leftHeight > rightHeight?leftHeight: rightHeight);}//返回indexPath所对应的cell的属性- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{ UICollectionViewLayoutAttributes * attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; CGFloat screenWidth = [[UIScreen mainScreen] bounds].size.width; PicModel * model = _dataArray[indexPath.row]; CGFloat leftHeight = 0.0f;//左边的总高度 CGFloat rightHeight = 0.0f;//右边的总高度 for (int i = 0; i < indexPath.row; i++) { PicModel * subModel = _dataArray[i]; if (leftHeight <= rightHeight) { //放左侧 leftHeight += subModel.height; }else{ rightHeight += subModel.height; } } if (leftHeight <= rightHeight) { //放左侧 attr.center = CGPointMake(screenWidth/4, leftHeight + model.height/2); }else{ attr.center = CGPointMake(screenWidth/4 * 3, rightHeight + model.height/2); } attr.size = CGSizeMake(screenWidth/2, model.height); return attr;}//返回的数组,每一个元素和与之序号对应的cell的属性- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{ NSMutableArray * arr = [[NSMutableArray alloc] init]; for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) { NSIndexPath * indexPath = [NSIndexPath indexPathForItem:i inSection:0]; UICollectionViewLayoutAttributes * attr = [self layoutAttributesForItemAtIndexPath:indexPath]; [arr addObject:attr]; } return arr;}@end
程序截图:
初始:
拉到最下面:
附:本程序代码:https://github.com/dzonel/collectionView
0 0
- UICollectionView瀑布流的实现,带例子
- UICollectionView的瀑布流实现
- UICollectionView实现瀑布流
- 用UICollectionView实现瀑布流
- iOS开发之瀑布流的实现(UICollectionView与UIScrollView)
- IOS开发之瀑布流的实现(UICollectionView与UIScrollView)
- 极客学院Carol老师的UICollectionView实现瀑布流
- ios 应用UICollectionView实现瀑布流
- ios 应用UICollectionView实现瀑布流
- iOS UICollectionView实现瀑布流(3)
- 利用UICollectionView 实现 页面瀑布流效果
- iOS UICollectionView实现瀑布流(3)
- iOS 使用UICollectionView实现瀑布流
- 使用UICollectionView 重写UICollectionViewLayout实现瀑布流
- iOS开发-UICollectionView实现瀑布流
- ios基于UICollectionView的 横向瀑布流
- UICollectionView瀑布流制作
- iOS UICollectionView瀑布流
- 前缀和 Codeforces509E Pretty Song
- 快速掌握Lua 5.3 —— 编译,运行以及错误
- beaglebone black hardware version
- android 电话状态的监听(来电和去电)
- 分享使用MFCC和LPC进行语音识别
- UICollectionView瀑布流的实现,带例子
- 2865: 结构体--日期计算
- Eclipse 连接MySql数据库总结
- 【寒假记录】素材收集整理
- 冒泡/下沉排序
- mahout in Action2.2-聚类介绍-K-means聚类算法
- 相对分子质量
- linux下安装软件
- C#OOP基础之类,字段,属性和方法。