iOS——UICollectionView

来源:互联网 发布:减肥从130到90 知乎 编辑:程序博客网 时间:2024/06/06 02:13

一、UICollectionView概述

继承与UIScrollView的一个视图,也称为集合视图。
UICollectionView的实现跟tableView不一样的地方在于Item的布局稍微复杂一点,需要用UICollectionViewLayout类来描述视图的布局。我们在项目中常用的是系统提供的UICollectionViewLayout类,也可以自定义UICollectionViewLayout。

二、UICollectionViewLayout

设置每个item的大小

@property (nonatomic) CGSize itemSize;

设置每个item的最小行间距(默认是10)

@property (nonatomic) CGFloat minimumLineSpacing;

设置每个itme的最小列间距(默认是10)

@property (nonatomic) CGFloat minimumInteritemSpacing;

头部引用的尺寸

@property (nonatomic) CGSize headerReferenceSize;

尾部引用的尺寸

@property (nonatomic) CGSize footerReferenceSize;

设置UICollectionView的滑动方向

@property (nonatomic) UICollectionViewScrollDirection scrollDirection;typedef NS_ENUM(NSInteger, UICollectionViewScrollDirection) {    UICollectionViewScrollDirectionVertical,  //垂直    UICollectionViewScrollDirectionHorizontal //水平};

三、UICollectionView

1.属性

初始化

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;

注册cell

-(void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;-(void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;-(void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;-(void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;

重用cell

-(__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;-(__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;

刷新collection

-(void)reloadData; 

2.代理方法(必须)

返回个数

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;

返回cell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

3.代理方法(可选)

点击方法

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

分区数

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;

四、返回头部、尾部视图样式

UICollectionView不能像UITableView一样直接指定头部和尾部视 图,需要注册使⽤用,最⼤大的好处是添加了重⽤用机制。

1.注册头部、尾部视图

[self.collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];[self.collectionView registerNib:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView"];

2.返回头部、尾部视图的样式

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{    //需要判断是否返回头部视图还是尾部视图    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {        //初始化头部视图        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];        //设置头部样式        headerView.backgroundColor = [UIColor redColor];        //返回头部视图        return headerView;    }else{        if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {            //初始化尾部视图            UICollectionReusableView *footView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footView" forIndexPath:indexPath];            //设置尾部样式            footView.backgroundColor = [UIColor yellowColor];             //返回尾部视图            return footView;        }    } }
0 0
原创粉丝点击