UICollectionView

集合视图由3部分组成。

Cell:单元格。
Supplementary View:补充视图,指的就是图中的Header与Footer,每个段Section都可以设置不同的补充视图。
Decoration View:装饰视图,一般是用于背景。

与表视图相比,集合视图引入了布局的概念,集合视图上的单元格布局方式是可以通过定制实现的,表视图中所有的单元格都是自上而下依次排列的,而集合视图中可以灵活定制单元格的布局方式单元格的布局设置是集合视图的重要特点。
集合视图中布局方式由其UICollectionViewLayout属性决定,系统提供了提供了以下两种布局方式。
UICollectionViewFlowLayout:Flow Layout是一个单元格的线性布局方案,并具有页面和页脚。这是一种比较常用的布局方式,即单元格依次排列。
自定义布局方式。这种布局方式具有很大的灵活性,但需要开发者创建一个UICollectionViewLayout类型的对象,并针对布局编写相关的代码。

集合视图继承自UIScrollView,因此其具有其父类的所有属性和方法。除此之外,集合视图还具有以下几个特殊的属性。

// 布局对象,用于实现单元格的自定义布局。
@property(nonatomic, strong) UICollectionViewLayout *collectionViewLayout;
// 集合视图的初始化方法,需要传递一个UICollectionViewLayout类型的布局对象。
-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
// 背景视图,会自动填充整个UICollectionView。
@property(nonatomic, strong) UIView* backgroundView;
// 是否允许选中cell,默认允许选中。
@property(nonatomic) BOOL allowsSelectin;
// 是否可以多选,默认只是单选。
@property(nonatomic) BOOL allowsMultipleSelection;

UICollectionView数据源协议DataSource

UICollectionView的数据源对象为集合视图的单元格Cell提供了数据,其基本使用方法与UITableView的数据源方法类似,但区别在于集合视图需要提前注册单元格Cell。

1.常用数据源方法集合视图的数据源协议为UICollectionViewDataSource,其中定义了用于为集合视图中的单元格提供数据的相关方法。其中,以下两个方法是必须实现的方法。
// 返回单元格个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
// 根据indexPath属性,返回该单元格的具体样式和显示内容。
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
另外,与表视图的数据源协议类似,这里还可以设置集合视图中包含的段Section数量。
// 返回段Section个数,默认为1。
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
// 设置头尾视图,同样也要先注册。
-(UICollectionReuseableView *)collectionView:(UICollectionView *)collectionView viewForSuppelementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
// 设置单元格是否可以移动。
-(BOOL)collectionView:(UICollectionView *)collectionView canmoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 获取被移动单元格的原始位置与最新位置,移动单元格后调用,此方法用于更新数据源中的数据。
-(void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)dextinationIndexPath NS_AVAILABLE(9_0);

在集合视图中的单元格必须在创建集合视图时进行注册,单元格有两种注册方式,分别对应通过代码创建的单元格以及通过XIB创建的单元格。

// 通过代码创建出的单元格的注册方法。
-(void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
// 通过xib创建出的单元格的注册方法。
-(void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
@interface ViewController ()<UICollectionViewDataSource>
@property(nonatomic, strong)UICollectionView* collectionView;
@end
当通过代码创建UICollectionView对象的时候,必须传递一个UICollectionViewLayout类型的对象作为参数
注意,这里提前创建的是UICollectionViewFlowLayout对象,而不是UICollectionViewLayout对象。UICollectionViewLayout是UICollectionViewFlowLayout的父类。
-(UICollectionView *)collectionView {
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UIcollectionViewFlowLayout alloc] init];
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}
// 注册单元格
-(void)viewDidLaod {
    [super viewDidLoad];
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
// 数据源方法的实现

UICollectionView代理协议方法简介

通过UICollectionView的代理协议UICollectionViewDelegate中定义的方法,可以捕获用户点击集合视图中单元格的行为,从而可以进行进一步处理。

1.与用户点击相关的代理方法
// 单元格被选中时调用。
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
// 设置单元格是否能够被选中。
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
// 设置单元格是否能够取消选中。
-(BOOL)collectionView:(UICollectionView *)collectionView shouldDeSelectItemAtIndexPath:(NSIndexPath *)indexPath;
// 单元格item被取消选中时调用。
-(void)collectionView:(UICollectionView *)collectionView didDeSelectItemAtIndexPath:(NSIndexPath *)indexPath;
2.与单元格高亮显示相关的代理方法
// 设置单元格是否能够高亮显示。
-(BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
// 单元格为高亮状态时调用,只有当collectionView:shouldHighlightItemAtIndexPath:方法返回YES时,此方法才有用。
-(void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
// 单元格取消高亮状态时调用。
-(void)collectionView:(UICollectionView *)collectionView didUnHighlightItemAtIndexPath:(NSIndexPath *)indexPath;

实现单元格被选中时的代理方法。

// ...

UICollectionViewFlowLayout简介

UICollectionView将其子视图的位置,大小和外观的控制权委托给一个单独的布局对象UICollectionViewLayout。通过提供一个自定义布局对象,程序员几乎可以实现任何能想象到的布局。UICollectionViewLayout是一个抽象基类,在开发过程中需要使用其子类

// 单元格之间的最小行间距
@property(nonatomic)CGFloat ninimumLineSpacing;
// 单元格之间的最小列间距
@property(nonatomic)CGFloat ninimumInteritemSpacing;
// 尺寸
@property(nonatomic)CGSize itemSize;
// 段Section的四边距
@property(nonatomic) UIEdgeInsets sectionInset;

UICollectionViewDelegateFlowLayout类的代理方法实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值