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