UICollectionView
UICollectionView和UITableView十分相似
一般须遵守协议
< UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout >
1. 基本使用
(1)先创建, 创建的时候先创建FlowLayout,然后创建UICollectionView的时候设置layout
//创建Layout, 创建UICollectionView会使用 UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; //创建 self.myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; self.myCollectionView.delegate = self; self.myCollectionView.dataSource = self; [self.view addSubview:self.myCollectionView]; //注册cell, 必须加此句提前注册cell, 否则崩溃 [self.myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"myCell"];
(2)代理方法
#pragma mark - dataSource 代理方法 //此方法可以不写, 默认为1 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } //指定section中item的个数 - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 10; } // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath: - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCell" forIndexPath:indexPath]; if(indexPath.section==0) { cell.backgroundColor = [UIColor redColor]; } if(indexPath.section==1) { cell.backgroundColor = [UIColor greenColor]; } return cell; }
(3)FlowLayout 属性和设置
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//滚动方向 flowLayout.minimumLineSpacing = 10.0;//行间距(最小值) flowLayout.minimumInteritemSpacing = 10;//item间距(最小值) flowLayout.itemSize = CGSizeMake(50, 50);//item的大小 flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);//设置section的边距
cell核心代码
-(id)initWithFrame:(CGRect)frame { if(self = [super initWithFrame:frame]) { //创建ui _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)]; [self.contentView addSubview:_imageView]; } return self; }
UICollectionView使用基本代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#import "ViewController.h" //使用集合视图 @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout> @property (strong,nonatomic) UICollectionView *collectionView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self createCollectionView]; } -(void)createCollectionView { UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; //item 行间距 flowLayout.minimumLineSpacing = 40; //默认10 flowLayout.minimumInteritemSpacing = 20; //默认10 //设置统一的item大小 flowLayout.itemSize = CGSizeMake(50, 50); //默认50,50 flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical; //默认竖直滚动 flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); //创建集合视图 //参数1: 位置 //参数2: 传入布局对象, 集合视图item布局由布局对象控制 _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout]; _collectionView.dataSource = self; _collectionView.delegate = self; [self.view addSubview:_collectionView]; //注册cell [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"]; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 2; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; if(indexPath.section == 0) { cell.backgroundColor = [UIColor redColor]; } else { cell.backgroundColor = [UIColor greenColor]; } return cell; } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { double w = 20 + arc4random()%40; double h = 20 + arc4random()%40; return CGSizeMake(w, h); }
UICollectionViewLayout自定义布局
//重写方法,准备布局,添加初始化操作
- (void)prepareLayout { [super prepareLayout]; ....... }
//返回内容的大小,一般返回集合视图大小
- (CGSize)collectionViewContentSize { return self.collectionView.frame.size; }
//返回所有的item对应布局对象的数组
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
//传入item的位置,返回这个item的布局对象
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
圆形布局自定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #import <UIKit/UIKit.h> 2 //圆形布局 3 @interface MyCircleLayout : UICollectionViewLayout 4 @property (nonatomic)CGPoint center; 5 @property (nonatomic)CGFloat radius; 6 @property (nonatomic)CGSize itemSize; 7 @end 8 9 ============================================ 10 #import "MyCircleLayout.h" 11 @interface MyCircleLayout () 12 @property (nonatomic)NSInteger cellCount; 13 @end 14 @implementation MyCircleLayout 15 16 //重写方法,准备布局,添加初始化操作 17 - (void)prepareLayout 18 { 19 [super prepareLayout]; 20 21 CGSize size = self.collectionView.frame.size; 22 //center 23 if (CGPointEqualToPoint(self.center, CGPointZero)) { 24 self.center = CGPointMake(size.width/2, size.height/2); 25 } 26 if (self.radius == 0) { 27 self.radius = MIN(size.width/2, size.height/2)*0.8; 28 } 29 if (CGSizeEqualToSize(self.itemSize, CGSizeZero)) { 30 self.itemSize = CGSizeMake(50, 50); 31 } 32 33 34 //利用集合视图的dataSourse协议 35 self.cellCount = [self.collectionView numberOfItemsInSection:0]; 36 } 37 //返回内容的大小,一般返回集合视图大小 38 - (CGSize)collectionViewContentSize 39 { 40 return self.collectionView.frame.size; 41 } 42 //返回所有的item对应布局对象的数组 43 - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect 44 { 45 NSMutableArray *marr = [[NSMutableArray alloc] init]; 46 for (int i = 0; i < self.cellCount; i ++) { 47 NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0]; 48 [marr addObject:[self layoutAttributesForItemAtIndexPath:indexPath]]; 49 } 50 return marr; 51 } 52 //传入item的位置,返回这个item的布局对象 53 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath 54 { 55 UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath]; 56 //每个item可能有不同的大小,不同的位置 57 attr.size = self.itemSize; 58 59 //获取角度 60 double angle = M_PI*2*indexPath.item / self.cellCount; 61 attr.center = CGPointMake(self.center.x+self.radius*cosf(angle), self.center.y+self.radius*sinf(angle)); 62 63 return attr; 64 } 65 66 @end