网格的适配横屏竖屏
注意适配加入iOS第三方 Masonry 图片定义从零开始
//AppDelegate.m
#import "ViewController.h"
ViewController *view = [[ViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];
self.window.rootViewController = nav;
//ViewController.h 继承UIViewController
//ViewController.m
#import "MyCell.h"
#import "HeaderReusableView.h"
#import "FooterReusableView.h"
#import "newViewController.h"
<UICollectionViewDataSource,UICollectionViewDelegate>
//============================================================
self.view.backgroundColor = [UIColor yellowColor];
// 创建流布局对象 (保存设定的样式规则)
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
// 设定单元格的大小
flow.itemSize = CGSizeMake(80, 100);
// 设定滚动方向
flow.scrollDirection = UICollectionViewScrollDirectionVertical;
// 最小列间距
flow.minimumInteritemSpacing = 10;
// 最小行间距
flow.minimumLineSpacing = 10;
// 设定分区的边距
flow.sectionInset = UIEdgeInsetsMake(10, 10, 20, 10);
// 设置页眉的尺寸
flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
// 设置页脚的尺寸
flow.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
// 创建网格对象,用流布局对象进行初始化
UICollectionView *collect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
// 设定代理关系
collect.dataSource = self;
collect.delegate = self;
collect.pagingEnabled = YES;
// 将网格加入到视图上显示
[self.view addSubview:collect];
// 为网格注册单元格类
[collect registerClass:[MyCell class] forCellWithReuseIdentifier:@"MyCell"];
// 为网格注册页眉类
[collect registerClass:[HeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
// 为网格注册页脚类
[collect registerClass:[FooterReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView"];
//=============================================================
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 4;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section==0)
return 1;
else if(section == 1)
return 10;
else if(section == 2)
return 4;
else if(section == 3)
return 6;
return 0;
}
// 单元格内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCell" forIndexPath:indexPath];
// 设置显示数据
cell.img.image = [UIImage imageNamed:@"3.jpg"];
cell.textLab.text = @"相守";
return cell;
}
// 设置页眉或页脚
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *resubleV = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
// 获取可重用的页眉对象,若是没有,就自动创建
resubleV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
HeaderReusableView *header = (HeaderReusableView*)resubleV;
header.titleLab.text = @"我是页眉";
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
{
resubleV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FooterView" forIndexPath:indexPath];
FooterReusableView *footer = (FooterReusableView*)resubleV;
footer.titleLab.text = @"我是页脚";
}
return resubleV;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"section:%ld, row:%ld",indexPath.section,indexPath.row);
newViewController *n = [[newViewController alloc] init];
[self.navigationController pushViewController:n animated:YES];
}
//HeaderReusableView.h 继承UICollectionReusableView
// 页眉的文字显示标签
@property (nonatomic,strong) UILabel *titleLab;
//HeaderReusableView.m
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.titleLab];
}
return self;
}
- (UILabel *)titleLab
{
if (!_titleLab )
{
_titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 200, 20)];
[_titleLab setTextColor:[UIColor whiteColor]];
}
return _titleLab;
}
//FooterReusableView.h 继承UICollectionReusableView
// 页脚的文字显示标签
@property (nonatomic,strong) UILabel *titleLab;
//FooterReusableView.m
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
[self addSubview:self.titleLab];
}
return self;
}
- (UILabel *)titleLab
{
if (!_titleLab)
{
_titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
[_titleLab setTextColor:[UIColor whiteColor]];
}
return _titleLab;
}
//MyCell.h
继承UICollectionViewCell
// 图片
@property (nonatomic,strong) UIImageView *img;
// 文字
@property (nonatomic,strong) UILabel *textLab;
//MyCell.m
-(instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame] ) {
[self addSubview:self.img];
[self addSubview:self.textLab];
}
return self;
}
- (UIImageView *)img
{
if (!_img)
{
_img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
[_img.layer setCornerRadius:80/2];
_img.layer.masksToBounds = YES;
}
return _img;
}
//
- (UILabel *)textLab
{
if (!_textLab)
{
_textLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, 80, 20)];
_textLab.textAlignment = NSTextAlignmentCenter;
_textLab.textColor = [UIColor whiteColor];
}
return _textLab;
}