在做CollectionView横屏适配之前需要导入第三方库Masonry,这个第三方还是很好用的.
ViewController.m
#import "myCollectionViewCell.h"//需要重写
#import
#import "Head.h"//页眉
#import "Foot.h"//页脚
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//创建
UICollectionViewFlowLayout *bj = [[UICollectionViewFlowLayout alloc]init];
bj.itemSize = CGSizeMake(80, 80);
bj.headerReferenceSize = CGSizeMake(40, 30);
bj.footerReferenceSize = CGSizeMake(40, 30);
//创建CollectionView对象
UICollectionView *cv = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:bj];
cv.delegate = self;
cv.dataSource = self;
[self.view addSubview:cv];
//适配可横屏
[cv mas_makeConstraints:^(MASConstraintMaker* make)
{
make.edges.equalTo(self.view);
}];
//为CollectionView注册单元格
[cv registerClass:[myCollectionViewCell class] forCellWithReuseIdentifier:@"myCollectionViewCell"];
//为CollectionView注册页眉类
[cv registerClass:[Head class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head"];
//为CollectionView注册页脚类
[cv registerClass:[Foot class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Foot"];
}
//设置页眉或页脚
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
UICollectionReusableView * theReusable = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
//获取可以重用的页眉对象,如果没有,就自动创建
theReusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Head" forIndexPath:indexPath];
Head * theHeader = (Head *)theReusable;
theHeader.theTitleLabel.text = @"页眉";
}else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
theReusable = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Foot" forIndexPath:indexPath];
Foot * theFooter = (Foot *)theReusable;
theFooter.theTitleLabel.text = @"页脚";
}
return theReusable;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 30;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
myCollectionViewCell *theCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myCollectionViewCell" forIndexPath:indexPath];
theCell.theImg.image = [UIImage imageNamed:@"1.jpg"];
return theCell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section:%ld, row:%ld",indexPath.section,indexPath.row);
}
myCollectionViewCell.h
#import
@interface myCollectionViewCell : UICollectionViewCell
@property(nonatomic,strong)UIImageView * theImg;
//@property(nonatomic,strong)UILabel * theLabel;
@end
.m重写
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.theImg];
// [self addSubview:self.theLabel];
}
return self;
}
-(UIImageView *)theImg{
if (!_theImg) {
_theImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
_theImg.layer.cornerRadius = 40;
[_theImg.layer setMasksToBounds:YES];
}
return _theImg;
}
Head.h
@interface Head : UICollectionReusableView
@property(nonatomic,strong)UILabel * theTitleLabel;
@end
.m
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.theTitleLabel];
}
return self;
}
-(UILabel *)theTitleLabel{
if (!_theTitleLabel) {
_theTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
_theTitleLabel.textColor = [UIColor whiteColor];
}
return _theTitleLabel;
}
Foot.h
@interface Foot : UICollectionReusableView
@property(nonatomic,strong)UILabel * theTitleLabel;
@end
.m
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.theTitleLabel];
}
return self;
}
-(UILabel *)theTitleLabel{
if (!_theTitleLabel) {
_theTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
_theTitleLabel.textColor = [UIColor whiteColor];
}
return _theTitleLabel;
}
其实CollectionView还是很好做的,与tableView近乎百分百相似,只要掌握tableView后,CollectionView也是很容易就上手的.