UICollectionView
和UICollectionViewController
类是iOS6新引进的API,用于展现集合视图,布局更加灵活,可实现多列布局,用法相似于UITableView
和UITableViewController
类,但也有所不一样。
UICollectionView
能够实现以下效果,也是一个经常使用的控件:
git
UICollectionView
的建立和UITableView
的建立有所不一样:github
UITableView
的建立只须要设置frame
便可使用UICollectionView
除了须要frame
,还须要一个布局参数-(id)initWithFrame:(CGRect)frame /* 尺寸 */ collectionViewLayout:(UICollectionViewLayout *)layout;/* 布局参数 */
UITableView
能够不须要注册Cell视图类,手动建立Cell视图类UICollectionView
必须注册视图类,才能显示,不须要手动建立UICollectionViewLayout
类的对象,UICollectionViewFlowLayout
scrollDirection
:typedef NS_ENUM(NSInteger, UICollectionViewScrollDirection) { UICollectionViewScrollDirectionVertical, /*垂直滚动*/ UICollectionViewScrollDirectionHorizontal /* 水平滚动 */ };
UITableView
不一样,UICollectionView
只能在这里设置顶部视图和底部视图的大小UICollectionView
的宽度,没法设置UICollectionView
的高度,没法设置/* 向容器视图注册Cell方块视图,有2种方式,一种是类名注册,一种是Xib注册 */ - (void)registerClass:(Class)cellClass /* 视图类 */ forCellWithReuseIdentifier:(NSString *)identifier;/* 绑定标识 */ - (void)registerNib:(UINib *)nib /* Xib */ forCellWithReuseIdentifier:(NSString *)identifier;/* 绑定标识 */ /* 从缓存池中取出Cell方块视图对象,若是缓存池没有,自动调用alloc/initWithFrame建立 */ - (UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath; /* kind参数设置 */ NSString *const UICollectionElementKindSectionHeader;/* 顶部视图用这个 */ NSString *const UICollectionElementKindSectionFooter;/* 底部视图用这个 */ /* 向容器视图注册顶部视图或者底部视图,有2种方式,一种是类名注册,一种是Xib注册 */ - (void)registerClass:(Class)viewClass forSupplementaryViewOfKind:(NSString *)kind /* 参考上面 */ withReuseIdentifier:(NSString *)identifier;/* 绑定标识 */ - (void)registerNib:(UINib *)nib forSupplementaryViewOfKind:(NSString *)kind /* 参考上面 */ withReuseIdentifier:(NSString *)identifier;/* 绑定标识 */ /* 从缓存池中取出顶部视图对象或者底部视图对象,若是缓存池没有,自动调用alloc/initWithFrame建立 */ - (UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
@required /* 设置容器视图各个组都有多少个Cell方块 */ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section; /* 设置Cell方块视图,相似于UITableViewCell的设置 */ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath; @optional /* 容器视图有多少个组,默认返回1 */ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView; /* 设置顶部视图和底部视图,经过kind参数分辨是设置顶部仍是底部 */ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
/* 选中Cell方块时调用 */ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath; /* 取消选中Cell方块时调用 */ - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
咱们使用更多的是UICollectionViewDelegate
子协议UICollectionViewDelegateFlowLayout
该协议不只包含父协议全部方法,还能够进行一些布局设置缓存
/* 设置每一个方块的尺寸大小 */ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath; /* 设置方块视图和边界的上下左右间距 */ - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
#import <UIKit/UIKit.h> @interface LTCollectionViewCell : UICollectionViewCell @property (strong, nonatomic) UILabel *textLabel; /* 方块视图的缓存池标示 */ + (NSString *)cellIdentifier; /* 获取方块视图对象 */ + (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath; @end
#import "LTCollectionViewCell.h" @implementation LTCollectionViewCell /* 方块视图的缓存池标示 */ + (NSString *)cellIdentifier{ static NSString *cellIdentifier = @"CollectionViewCellIdentifier"; return cellIdentifier; } /* 获取方块视图对象 */ + (instancetype)cellWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath { //从缓存池中寻找方块视图对象,若是没有,该方法自动调用alloc/initWithFrame建立一个新的方块视图返回 LTCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[LTCollectionViewCell cellIdentifier] forIndexPath:indexPath]; return cell; } /* 注册了方块视图后,当缓存池中没有底部视图的对象时候,自动调用alloc/initWithFrame建立 */ - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { //建立label UILabel *textLabel = [[UILabel alloc] init]; //设置label尺寸 CGFloat x = 5; CGFloat y = 5; CGFloat width = frame.size.width - 10; CGFloat height = frame.size.height - 10; textLabel.frame = CGRectMake(x, y, width, height); //设置label属性 textLabel.numberOfLines = 0; textLabel.textAlignment = NSTextAlignmentCenter; textLabel.font = [UIFont systemFontOfSize:15]; //添加到父控件 [self.contentView addSubview:textLabel]; self.textLabel = textLabel; } return self; } @end
#import <UIKit/UIKit.h> @interface LTCollectionHeaderView : UICollectionReusableView @property (strong, nonatomic) UILabel *textLabel; /* 顶部视图的缓存池标示 */ + (NSString *)headerViewIdentifier; /* 获取顶部视图对象 */ + (instancetype)headerViewWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath; @end
#import "LTCollectionHeaderView.h" @implementation LTCollectionHeaderView /* 顶部视图的缓存池标示 */ + (NSString *)headerViewIdentifier{ static NSString *headerIdentifier = @"headerViewIdentifier"; return headerIdentifier; } /* 获取顶部视图对象 */ + (instancetype)headerViewWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath { //从缓存池中寻找顶部视图对象,若是没有,该方法自动调用alloc/initWithFrame建立一个新的顶部视图返回 LTCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[LTCollectionHeaderView headerViewIdentifier] forIndexPath:indexPath]; return headerView; } /* 注册了顶部视图后,当缓存池中没有顶部视图的对象时候,自动调用alloc/initWithFrame建立 */ - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { //建立label UILabel *textLabel = [[UILabel alloc] init]; //设置label尺寸 CGFloat x = 5; CGFloat y = 5; CGFloat width = frame.size.width - 10; CGFloat height = frame.size.height - 10; textLabel.frame = CGRectMake(x, y, width, height); //设置label属性 textLabel.numberOfLines = 0; textLabel.textAlignment = NSTextAlignmentCenter; //添加到父控件 [self addSubview:textLabel]; self.textLabel = textLabel; } return self; } @end
#import <UIKit/UIKit.h> @interface LTCollectionFooterView : UICollectionReusableView @property (strong, nonatomic) UILabel *textLabel; /* 底部视图的缓存池标示 */ + (NSString *)footerViewIdentifier; /* 获取底部视图对象 */ + (instancetype)footerViewWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath; @end
#import "LTCollectionFooterView.h" @implementation LTCollectionFooterView /* 底部视图的缓存池标示 */ + (NSString *)footerViewIdentifier{ static NSString *footerIdentifier = @"footerViewIdentifier"; return footerIdentifier; } /* 获取底部视图对象 */ + (instancetype)footerViewWithCollectionView:(UICollectionView *)collectionView forIndexPath:(NSIndexPath *)indexPath { //从缓存池中寻找底部视图对象,若是没有,该方法自动调用alloc/initWithFrame建立一个新的底部视图返回 LTCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[LTCollectionFooterView footerViewIdentifier] forIndexPath:indexPath]; return footerView; } /* 注册了底部视图后,当缓存池中没有底部视图的对象时候,自动调用alloc/initWithFrame建立 */ - (instancetype)initWithFrame:(CGRect)frame{ if (self = [super initWithFrame:frame]) { //建立label UILabel *textLabel = [[UILabel alloc] init]; //设置label尺寸 CGFloat x = 5; CGFloat y = 5; CGFloat width = frame.size.width - 10; CGFloat height = frame.size.height - 10; textLabel.frame = CGRectMake(x, y, width, height); //设置label属性 textLabel.numberOfLines = 0; textLabel.textAlignment = NSTextAlignmentCenter; //添加到父控件 [self addSubview:textLabel]; self.textLabel = textLabel; } return self; } @end
#import "ViewController.h" #import "LTCollectionViewCell.h" #import "LTCollectionHeaderView.h" #import "LTCollectionFooterView.h" @interface ViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout> @property (strong, nonatomic) UICollectionView *collectionView;/*< 容器视图 */ @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化容器视图 [self initCollectionView]; }
/* 初始化容器视图 */ - (void)initCollectionView { CGFloat x = 0; CGFloat y = 20; CGFloat width = self.view.frame.size.width; CGFloat height = self.view.frame.size.height - 20; //建立布局对象 UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; //设置滚动方向为垂直滚动,说明方块是从左上到右下的布局排列方式 layout.scrollDirection = UICollectionViewScrollDirectionVertical; //设置顶部视图和底部视图的大小,当滚动方向为垂直时,设置宽度无效,当滚动方向为水平时,设置高度无效 layout.headerReferenceSize = CGSizeMake(100, 40); layout.footerReferenceSize = CGSizeMake(100, 40); //建立容器视图 CGRect frame = CGRectMake(x, y, width, height); UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:frame collectionViewLayout:layout]; collectionView.delegate = self;//设置代理 collectionView.dataSource = self;//设置数据源 collectionView.backgroundColor = [UIColor whiteColor];//设置背景,默认为黑色 //添加到主视图 [self.view addSubview:collectionView]; self.collectionView = collectionView; //注册容器视图中显示的方块视图 [collectionView registerClass:[LTCollectionViewCell class] forCellWithReuseIdentifier:[LTCollectionViewCell cellIdentifier]]; //注册容器视图中显示的顶部视图 [collectionView registerClass:[LTCollectionHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:[LTCollectionHeaderView headerViewIdentifier]]; //注册容器视图中显示的底部视图 [collectionView registerClass:[LTCollectionFooterView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:[LTCollectionFooterView footerViewIdentifier]]; }
#pragma mark - UICollectionViewDataSource /* 设置容器中有多少个组 */ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 10; } /* 设置每一个组有多少个方块 */ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 20; } /* 设置方块的视图 */ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { //获取cell视图,内部经过去缓存池中取,若是缓存池中没有,就自动建立一个新的cell LTCollectionViewCell *cell = [LTCollectionViewCell cellWithCollectionView:collectionView forIndexPath:indexPath]; //设置cell属性 cell.contentView.backgroundColor = [UIColor redColor]; cell.textLabel.text = [NSString stringWithFormat:@"Cell %2ld",indexPath.row]; return cell; } /* 设置顶部视图和底部视图 */ - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if ( [kind isEqualToString:UICollectionElementKindSectionHeader] ) {//顶部视图 //获取顶部视图 LTCollectionHeaderView *headerView = [LTCollectionHeaderView headerViewWithCollectionView:collectionView forIndexPath:indexPath]; //设置顶部视图属性 headerView.backgroundColor = [UIColor orangeColor]; headerView.textLabel.text = [NSString stringWithFormat:@"-Header-%ld-",indexPath.section]; return headerView; } else if( [kind isEqualToString:UICollectionElementKindSectionFooter] ) {//底部视图 //获取底部视图 LTCollectionFooterView *footerView = [LTCollectionFooterView footerViewWithCollectionView:collectionView forIndexPath:indexPath]; //设置底部视图属性 footerView.backgroundColor = [UIColor greenColor]; footerView.textLabel.text = [NSString stringWithFormat:@"-Footer-%ld-",indexPath.section]; return footerView; } return nil; }
#pragma mark - UICollectionViewDelegateFlowLayout /* 设置各个方块的大小尺寸 */ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { CGFloat width = 50; CGFloat height = 50; return CGSizeMake(width, height); } /* 设置每一组的上下左右间距 */ - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(10, 10, 10, 10); }
#pragma mark - UICollectionViewDelegate /* 方块被选中会调用 */ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"点击选择了第%ld组第%ld个方块",indexPath.section,indexPath.row); } /* 方块取消选中会调用 */ - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"取消选择第%ld组第%ld个方块",indexPath.section,indexPath.row); } @end
/* 设置每一组的上下左右间距 */ - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 0); }
上面的代码Demo点这里:LearnDemo里面的CollectionViewDemoide