iOS UICollectionView学习之二,纯代码实现布局,纯代码实现添加Section Header头部视图和 Section Footer尾部视图

本文介绍了如何在iOS中使用UICollectionView纯代码实现自定义布局,并详细讲解了添加Section Header和Footer视图的过程,包括自定义单元格、头部视图和尾部视图的创建及在控制器中的使用方法。
摘要由CSDN通过智能技术生成

iOS UICollectionView学习之二,纯代码实现布局,纯代码实现添加头部视图跟尾部视图,Header和footer

1、首先看下整个Demo的整体结构:如下图



2、直接上代码

1.自定义单元格类----------MyCell.h  里面添加

@interface MyCell :UICollectionViewCell

@property(strong,nonatomic)UIImageView *imageV;

@property(strong,nonatomic)UILabel *titleLab;

@end


2.自定义单元格类----------MyCell.m  里面实现

-(instancetype)initWithFrame:(CGRect)frame

{

   self = [superinitWithFrame:frame];

   if (self)

   {

        //定义CELL单元格内容

        _imageV = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, 100,100)];

        _imageV.backgroundColor = [UIColorclearColor];

       [self addSubview:_imageV];

        

        _titleLab = [[UILabelalloc]initWithFrame:CGRectMake(0,100, 100, 30)];

        _titleLab.backgroundColor = [UIColorclearColor];

        _titleLab.textAlignment =NSTextAlignmentCenter;

       [self addSubview:_titleLab];    

   }

    returnself;

}


3.自定义头部视图------MyHeaderView.h 里面添加

@interface MyHeaderView :UICollectionReusableView

//添加一个lable用于显示内容

@property(strong,nonatomic)UILabel *titleLab;

@end


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值