iOS经典讲解之UICollectionView

本文深入探讨了如何在iOS应用中使用UICollectionView并配置布局,包括初始化、布局配置、代理方法实现以及重用cell和表头表尾的注册。

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

//
//  RootViewController.m
//  LessonUICollectionView
//

#import "RootViewController.h"

@interface RootViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
//UICollectionViewDelegateFlowLayout该类遵守了UICollectionViewDelegate
// 实际上UICollectionViewDelegateFlowLayout这个协议是UICollectionViewDelegate的子协议

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addSubViews];
    
}
// 创建一个集合视图 collectionView
- (void)addSubViews
{
    // 初始化UICollectionView需要一个独立的类UICollectionViewFlowLayout来设置其布局
    //UICollectionViewLayout是一个抽象类 本身没有具体功能 其功能是由其子类UICollectionViewFlowLayout来实现的
    // Item 布局(网格状 布局)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 行边距(相对于 上下滑动) 如果左右滑动 就是列边距
    layout.minimumLineSpacing = 30;
    // 列边距(相对于 上下滑动) 如果左右滑动 就是行边距
    layout.minimumInteritemSpacing = 30;
    // 设置Item的宽高
    layout.itemSize = CGSizeMake(150, 200);
    // 设置滑动方向(默认是上下滑动的)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    // 设置表头  宽度不起作用
    layout.headerReferenceSize = CGSizeMake(0, 100);
    // 设置表尾  宽度不起作用
    layout.footerReferenceSize = CGSizeMake(0, 100);
    // 设置内边距
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    

    // 用上面的layout布局初始化集合视图
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout];
    //设置代理
    collectionView.dataSource = self;
    collectionView.delegate = self;
    // collectionView默认是黑的
    collectionView.backgroundColor = [UIColor whiteColor];
    // 显示视图
    [self.view addSubview:collectionView];
    [collectionView release];
    [layout release];
    
    
    // 注册你要用的cell
    //Identifier 重用标示符一定要和下面的一致
    //registerClass:<#(Class)#> cell的类 系统的或你自定义的cell所属类
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"MyCell"];
    
    // 注册表头
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView"];
    // 注册表尾
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView"];
    
}
#pragma mark ------代理方法------
// 必须实现的两个方法 跟tableView一样
// 返回每个分区的Item数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 5;
}
// 返回每个Item的方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 返回每个Item的方法
    // 这个方法里面包括了之前tableViewCell写的那一大堆 不需要再alloc创建,如果集合中有会取出复用如果没有他会自动创建不需要自己再创建
    // 必须要注册cell
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];

    // 系统没有像tableView一样 给咱们 提供布局方式
    // 要使用UICollectionViewCell一般都是自定义再使用 跟tableView一样所有自定义控件都要加到contentView上
    cell.contentView.backgroundColor = [UIColor redColor];
    
    return cell;
    
}
// 返回分区数跟tableView一样 默认一个
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 设置表头 表尾 同一个代理方法来是实现
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    // 有参数kind判断返回表头还是表尾
    // 因为参数是字符串的 判断相同不能等号要用方法isEqualToString:
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 返回表头 需要去复用集合中得到
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MyHeaderView" forIndexPath:indexPath];
        // 加颜色
        headerView.backgroundColor = [UIColor greenColor];
        return headerView;
        
    } else {
        // 返回表尾
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"MyFooterView" forIndexPath:indexPath];
        footerView.backgroundColor = [UIColor yellowColor];
        return footerView;
    }
}


@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值