iOS:UICollectionView的子类化创建

本文介绍了如何在iOS开发中创建并自定义UICollectionView的子类,通过设置属性和使用不同的flowLayout实现多样化的布局效果。

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

UICollectionView的创建基本与UITableView的创建方式相同

首先,创建继承于UICollectionView的子类

然后在初始化方法中设置一些属性

- (id)initWithFrame:(CGRect)frame
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumInteritemSpacing = 0; //列间距
    flowLayout.minimumLineSpacing = 0;      //行间距
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        //隐藏滑块
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        //设置代理
        self.delegate = self;
        self.dataSource = self;
        //设置背景颜色(默认黑色)
        self.backgroundColor = [UIColor whiteColor];
        //注册单元格
        [self registerClass:[YSStudentStatusCell class] forCellWithReuseIdentifier:identify];
    }
    return self;
}

collectionView的协议方法基本与tableView的相同,主要区别在于cell的创建与头视图的创建

先看cell的创建

<p class="p1"></p><p class="p1">//创建cell</p><p class="p1">- (<span class="s1">UICollectionViewCell</span> *)collectionView:(<span class="s1">UICollectionView</span> *)collectionView cellForItemAtIndexPath:(<span class="s1">NSIndexPath</span> *)indexPath</p><p class="p1">{</p><p class="p1">    <span class="s2">ModelStudent</span> *model = <span class="s3">self</span>.<span class="s2">dataArray</span>[indexPath.<span class="s1">item</span>];</p><p class="p1">    //子类化的cell</p><p class="p2"><span class="s4">    </span><span class="s2">YSStudentStatusCell</span><span class="s4"> *cell = [collectionView </span>dequeueReusableCellWithReuseIdentifier<span class="s4">:</span><span class="s2">identify</span><span class="s4"> </span>forIndexPath<span class="s4">:indexPath];</span></p><p class="p1">    [cell <span class="s5">configureWithModel</span>:model.<span class="s2">StuUserInfo</span>];</p><p class="p1">    <span class="s3">return</span> cell;</p><p class="p1">}</p>

collectionView的子类化cell创建的初始化方法如下,我用init不执行

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}

collectionView头视图的创建,首先需要注册头视图,注册demo我一般与cell注册写在一块

//collection头视图的注册
        [self registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Identifierhead"];
创建头视图的协议方法

//组的头视图创建
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Identifierhead" forIndexPath:indexPath];
    headView.backgroundColor = [UIColor blueColor];
    return headView;
}


另外说明一下 flowLayout 的使用

flowLayout可以说是collectionView的布局属性,设置不同 flowLayout ,可以加载出不同的collectionView的布局式样



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值