//ViewController.m
#import "ViewController.h"#import "HeaderCollectionReusableView.h"#import "FooterCollectionReusableView.h"#import "MyCell.h"@interface ViewController (){
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建流布局对象
UICollectionViewFlowLayout *Flow = [[UICollectionViewFlowLayout alloc]init];
//设定单元格的大小
Flow.itemSize = CGSizeMake(70, 100);
//设置滚动方向
Flow.scrollDirection = UICollectionViewScrollDirectionVertical;
//设置最小行间隔
Flow.minimumLineSpacing = 100;
//设置最小列间距
Flow.minimumInteritemSpacing = 10;
//设定分区的间距
Flow.sectionInset = UIEdgeInsetsMake(50, 10, 20, 10);
//设置页眉的尺寸
Flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
//设置页脚的尺寸
Flow.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
//创建网格对象
UICollectionView *theCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:Flow];
//
theCollect.delegate = self;
theCollect.dataSource = self;
//是否按页滑动
theCollect.pagingEnabled = YES;
theCollect.backgroundColor = [UIColor whiteColor];
[self.view addSubview:theCollect];
//注册单元格
[theCollect registerClass:[MyCell class] forCellWithReuseIdentifier:@"Mycell"];
//注册页眉
[theCollect registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
//注册页脚
[theCollect registerClass:[FooterCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
}
#pragma -UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section == 0)
{
return 5;
}
else if (section == 1)
{
return 10;
}
return 0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Mycell" forIndexPath:indexPath];
//
cell.Img.image = [UIImage imageNamed:@"2.jpg"];
cell.textLb.text = @"信";
return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
UICollectionReusableView *reusbleV = nil;
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
reusbleV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
HeaderCollectionReusableView *header = (HeaderCollectionReusableView *)reusbleV;
header.titleLb.text = @"我是页眉";
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter])
{
reusbleV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
FooterCollectionReusableView *Footer = (FooterCollectionReusableView *)reusbleV;
Footer.titleLb.text = @"我是页脚";
}
return reusbleV;
}
@end
//HeaderCollectionReusableView.h
#import@interface HeaderCollectionReusableView : UICollectionReusableView
@property (nonatomic,strong)UILabel *titleLb;
@end
//HeaderCollectionReusableView.m
#import "HeaderCollectionReusableView.h"
@implementation HeaderCollectionReusableView
//单例方法
- (instancetype)initWithFrame:(CGRect)frame
{
if (self == [super initWithFrame:frame])
{
[self addSubview:self.titleLb];
}
return self;
}
//懒加载
- (UILabel *)titleLb
{
if (!_titleLb)
{
_titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
[_titleLb setTextColor:[UIColor whiteColor]];
}
return _titleLb;
}
@end
//FooterCollectionReusableView.m
#import <UIKit/UIKit.h>
@interface FooterCollectionReusableView : UICollectionReusableView
@property (nonatomic,strong)UILabel *titleLb;
@end
//FooterCollectionReusableView.m
#import "FooterCollectionReusableView.h"
@implementation FooterCollectionReusableView
//单例方法
- (instancetype)initWithFrame:(CGRect)frame
{
if (self == [super initWithFrame:frame])
{
[self addSubview:self.titleLb];
}
return self;
}
//懒加载
- (UILabel *)titleLb
{
if (!_titleLb)
{
_titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 20)];
[_titleLb setTextColor:[UIColor whiteColor]];
}
return _titleLb;
}
@end
//MyCell.h
#import <UIKit/UIKit.h>
@interface MyCell : UICollectionViewCell
@property (nonatomic,strong)UIImageView *Img;
@property (nonatomic,strong)UILabel *textLb;
@end
//MyCell.m
#import "MyCell.h"
@implementation MyCell
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[self addSubview:self.Img];
[self addSubview:self.textLb];
}
return self;
}
- (UIImageView *)Img
{
if (!_Img)
{
_Img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)];
_Img.layer.cornerRadius = 80/2;
_Img.layer.masksToBounds = YES;
}
return _Img;
}
- (UILabel *)textLb
{
if (!_textLb)
{
_textLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 20)];
_textLb.textAlignment = NSTextAlignmentCenter;
_textLb.textColor = [UIColor blackColor];
}
return _textLb;
}
@end