UICollectionView

UICollectionView

An object that manages an ordered collection of data items and presents them using customizable layouts.

一个对象用来管理数据项的有序集合并可以通过自定义布局展示。

声明

@interface UICollectionView : UIScrollView

概述

UICollectionView

An object that manages an ordered collection of data items and presents them using customizable layouts.

一个对象用来管理数据项的有序集合并可以通过自定义布局展示。

声明

@interface UICollectionView : UIScrollView

概述

将collectionview添加到用户界面时,应用程序的主要工作是管理与collectionview关联的数据。collectionview从数据源对象获取数据,该对象是遵循 UICollectionViewDataSource协议的对象,由您的应用提供。集合视图中的数据被组织成单独的项目,然后可以将其分组为各个部分以进行展示。item是您要呈现的最小数据单位。item是UICollectionViewCell的实例。

UITableView和UICollectionView是相类似的,有着相同的API设计理念(基于DataSource和Delegate驱动)。

集合视图和布局对象

与集合视图关联的一个非常重要的对象是布局对象,它是该类UICollectionViewLayout的子类。布局对象负责定义集合视图中所有单元格和补充视图的组织和位置。尽管布局对象定义了它们的位置,但实际上并没有将该信息应用于相应的视图。因为单元格和辅助视图的创建涉及集合视图与数据源对象之间的协调,所以集合视图实际上将布局信息应用于视图。因此,从某种意义上说,布局对象就像另一个数据源,仅提供视觉信息而不是项目数据。

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout: flowLayout];

collectionview的创建需要布局对象,布局对象是UICollectionViewLayout的子类实例,仅仅为collectionview提供视图布局信息。

minimumInteritemSpacing 根据设定的itemSize和minimumInteritemSpacing来确定一行多少个item
minimumLineSpacing 用于设置行间距
itemSize 用于设定item的宽高大小

创建单元格和补充视图

集合视图的数据源对象既提供项目的内容,又提供用于呈现该内容的视图。集合视图首次加载其内容时,会要求其数据源为每个可见项提供一个视图,同样collectionview也有相应的重用机制。

使用可以在集合视图中获取项目的单元格
dequeueReusableCellWithReuseIdentifier:forIndexPath:

创建collectionviewcell之前必须对其cell进行注册,以用于cell的重用。
必须先注册 Cell 类型⽤用于重⽤

  1. - (void)registerClass:(Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
  2. - (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;

案例

使用系统提供的瀑布流效果。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 20;
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.itemSize = CGSizeMake((self.view.bounds.size.width - 30) / 2, 200);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout: flowLayout];
    // collectionView必须注册
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor systemPurpleColor];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item % 3 == 0) {
        return CGSizeMake(self.view.frame.size.width, 100);
    } else {
        return CGSizeMake((self.view.frame.size.width - 10)/2, 300);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值