
ViewController.m
#import "ViewController.h"
#define MAS_SHORTHAND_GLOBALS
#import"Masonry.h"
static NSString *const identifier = @"cell";
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *cv;
@end
@implementation ViewController
#pragma make - flowlayout delegate 协议方法
//设置各分区不同的行距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
if(section == 2 || section == 3){
return 0;
}
return 10;
}
//设置各分区不同的项间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
if(section == 2 || section == 3){
return 0;
}
return 10;
}
//设置不同分区的内边距
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
if(section == 2 || section == 3){
return UIEdgeInsetsZero;
}
return UIEdgeInsetsMake(10, 10, 10, 10);
}
//设置不同分区项大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.section) {
case 0:
return CGSizeMake(20, 20) ;
case 1:
return CGSizeMake(80, 80) ;
case 2:
return CGSizeMake(180, 180) ;
default:
return CGSizeMake(50, 50);
}
}
//设置不同的分区头视图的高度,如果高度设置为0,则表示没有分区头
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
switch (section) {
case 0:
return CGSizeMake(20, 20) ;
case 1:
return CGSizeMake(80, 80) ;
case 2:
return CGSizeMake(180, 180) ;
default:
return CGSizeMake(50, 50);}
}
//返回每个分区的 分区头 或分区脚的样式
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//分区头复用
if(kind == UICollectionElementKindSectionHeader){
UICollectionReusableView *rv = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell" forIndexPath:indexPath];
rv.backgroundColor = [UIColor whiteColor];
return rv;
}
//分区尾的复用
UICollectionReusableView *fv = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerCell" forIndexPath:indexPath];
fv.backgroundColor = [UIColor lightGrayColor];
return fv;
}
-(UICollectionViewFlowLayout*)createLayoutObject{
UICollectionViewFlowLayout *fLayout = [[UICollectionViewFlowLayout alloc]init];
fLayout.minimumLineSpacing = 10;
fLayout.minimumInteritemSpacing = 10;
fLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
CGFloat width = ([UIScreen mainScreen].bounds.size.width - 20*2 - 10*2)/3;
fLayout.itemSize = CGSizeMake(width, width);
//设置分区头高度
fLayout.headerReferenceSize =CGSizeMake(0, 40);
//设置分区脚高度
fLayout.footerReferenceSize =CGSizeMake(0, 20);
return fLayout;
}
-(UICollectionView *)cv{
if(!_cv){
_cv = [[UICollectionView alloc]initWithFrame:CGRectNull collectionViewLayout:[self createLayoutObject]];
[self.view addSubview:_cv];
_cv.dataSource = self;
_cv.delegate = self;
[_cv registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:identifier];
//注册复用的分区头样式
[_cv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headCell"];
//注册复用的分区尾样式
[_cv registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
withReuseIdentifier:@"footerCell"];
[_cv mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(UIEdgeInsetsMake(20, 0, 0, 0));
}];
}
return _cv;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 4;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
switch (section) {
case 0:
return 6;
case 1:
return 12;
case 3:
return 8;
default:
return 18;
}
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
cell.backgroundColor = [self randomColor];
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self cv];
}
-(UIColor*)randomColor{
CGFloat red = (arc4random()%256) /255.0;
CGFloat green = (arc4random()%256) /255.0;
CGFloat blue = (arc4random()%256) / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:1];
}
网友评论