iOS学习笔记33-UICollectionView入门

一、UICollectionView介绍

UICollectionViewUICollectionViewController类是iOS6新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableViewUITableViewController类,但也有所不同。
UICollectionView可以实现如下效果,也是一个常用的控件:

二、UICollectiomView使用

UICollectionView的创建和UITableView的创建有所不同:
1. UITableView的创建只需要设置frame即可使用
UICollectionView除了需要frame,还需要一个布局参数

-(id)initWithFrame:(CGRect)frame /* 尺寸 */
        collectionViewLayout:(UICollectionViewLayout *)layout;/* 布局参数 */
  1. UITableView可以不需要注册Cell视图类,手动创建Cell视图类
    UICollectionView必须注册视图类,才能显示,不需要手动创建
UICollectionView的布局参数:
  1. 是一个UICollectionViewLayout类的对象,
    但我们一般使用它的子类UICollectionViewFlowLayout
  2. 设置布局对象的滚动方向属性scrollDirection
typedef NS_ENUM(NSInteger, UICollectionViewScrollDirection) {     
        UICollectionViewScrollDirectionVertical,  /*垂直滚动*/  
        UICollectionViewScrollDirectionHorizontal /* 水平滚动 */
};
  1. 垂直滚动,表示Cell方块布局是从左往右,从上到下排列的布局
  2. 水平滚动,表示Cell方块布局是从上往下,从左到右排列的布局
  3. UITableView不同,UICollectionView只能在这里设置顶部视图和底部视图的大小
  4. 设置为垂直滚动时,顶部和底部视图的宽度为UICollectionView的宽度,无法设置
  5. 设置为水平滚动时,顶部和底部视图的高度为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;

UICollectionView的数据源方法
@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;
UICollectionViewDelegate的常用方法
/* 选中Cell方块时调用 */
- (void)collectionView:(UICollectionView *)collectionView 
        didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
/* 取消选中Cell方块时调用 */
- (void)collectionView:(UICollectionView *)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值