iOS开发:集合视图 UICollectionView

本文详细介绍了如何在 iOS 应用中使用 UICollectionView 来展示数据。包括创建 UICollectionView 的步骤、设置代理和数据源的方法、自定义 UICollectionViewCell 的过程,以及 UICollectionViewDelegateFlowLayout 代理的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        iOS:集合视图 UICollectionView

1.创建集合视图的步骤:

(1).使用系统的布局UICollectionViewFlowLayout

  //初始化标准瀑布流的对象

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];

    //每一块的大小

    flowLayout.itemSize = CGSizeMake(355, 655);

    //最小行间距

    flowLayout.minimumLineSpacing = 15;

    //最小列间距

    flowLayout.minimumInteritemSpacing = 15;

    //设置上左下右的距离

    flowLayout.sectionInset = UIEdgeInsetsMake(20, 15, 20, 15); //整个collectionView距离上左下右

    //滚动方向(默认是纵向vertical)

    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

   (2).设置代理,设置数据源

<UICollectionViewDataSource, UICollectionViewDelegate>

 //初始化CollectionView

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:flowLayout];

   _collectionView.dataSource = self;

    _collectionView.delegate = self;

   //整页滚动

   _collectionView.pagingEnabled = YES;

   [self.view addSubview:_collectionView];

(3).设置自定义cell

#warning 重点:collectionView 注册cell

   [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];

//实现协议方法:

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


    return _dataSourceArray.count; //返回块数

}


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


    //直接进重用池找可用cell

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];

cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 225.0 green:arc4random() % 256 / 225.0 blue:arc4random() % 256 / 225.0 alpha:1];

       return cell;

}


//获取当前cell的下标

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

    

    NSLog(@"%ld", indexPath.row);

}



(4).设置 <UICollectionViewDelegateFlowLayout>代理

@protocol UICollectionViewDelegateFlowLayout <UICollectionViewDelegate>

@optional


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;


@end








评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值