#import "ViewController.h
"#import "Mycell.h"
@interface ViewController ()@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//流布局
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);
//创建网格对象
UICollectionView *colle = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flow];
//设置代理
colle.delegate = self;
colle.dataSource = self;
//设置是否可以分页滑动
colle.pagingEnabled = YES;
//设置页眉的尺寸
flow.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 30);
//设置页脚的尺寸
flow.footerReferenceSize = CGSizeMake(self.view.frame.size.width, 10);
[self.view addSubview:colle];
//为网格进行注册
[colle registerClass:[Mycell class] forCellWithReuseIdentifier:@"Mycell"];
// //注册页眉类
// [colle registerClass:[HeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeadrView"];
//
// //注册页脚类
// [colle registerClass:[FootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"FootView"];
}
#pragma -
#pragma mark -UICollectionViewDelegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
if (section == 0)
{
return 10;
}
else if (section == 1)
{
return 20;
}
return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
Mycell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Mycell" forIndexPath:indexPath];
cell.img.image = [UIImage imageNamed:@"longing.png"];
cell.label.text = @"齐天大圣";
return cell;
}
#import@interface Mycell : UICollectionViewCell
//图片
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UIImageView *img;
@end
#import "Mycell.h"
@implementation Mycell
//单例方法
-(instancetype)initWithFrame:(CGRect)frame
{
//重写父类方法
if (self = [super initWithFrame:frame])
{
[self addSubview:self.img];
[self addSubview:self.label];
}
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 *)label
{
if (!_label)
{
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, 80, 80, 30)];
_label.font = [UIFont systemFontOfSize:12];
_label.textColor = [UIColor whiteColor];
_label.textAlignment = NSTextAlignmentCenter;
}
return _label;
}
@end