#import "ViewController.h"
#define ID @"reuse"#import "MyCollectionViewCell.h"#import "MyCollectionReusableView.h"#define kWidth self.view.frame.size.width@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>@end@implementation ViewController- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{ return CGSizeMake((kWidth - 40) / 3, 100);}- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { return UIEdgeInsetsMake(0, 0, 0, 0);}- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { return 20;}//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { // return 20;//}//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { // //}//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { // //}- (void)viewDidLoad { [super viewDidLoad]; //创建一个布局对象,采用系统布局类UICollectionViewFlowLayout UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // //设置最小的行间距// layout.minimumLineSpacing = 10;// // //设置Item之间间距// layout.minimumInteritemSpacing = 10;// //设置集合视图的分区间隔// layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); //设置集合视图的滑动方向 layout.scrollDirection = UICollectionViewScrollDirectionVertical; CGFloat totalWidth = self.view.frame.size.width; //设置每一个Item的尺寸大小// layout.itemSize = CGSizeMake((totalWidth - 40) / 3, 80); layout.headerReferenceSize = CGSizeMake(totalWidth, 40); //集合视图的创建必须指定布局,如果没有布局,显示不了任何东西 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:layout]; //集合视图如果想要显示内容,必须将cell注册 [collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:ID]; collectionView.dataSource = self; collectionView.delegate = self; //如果想要分区头视图显示,必须注册增广视图 [collectionView registerClass:[MyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"]; [self.view addSubview:collectionView]; collectionView.backgroundColor = [UIColor grayColor]; // Do any additional setup after loading the view, typically from a nib.}- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return 60;}// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // static NSString *ID = @"reuse"; MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath]; cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0]; cell.numberLabel.text = [NSString stringWithFormat:@"道无涯第%ld步",indexPath.row + 1]; return cell;}//设置分区个数- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 3;}//返回增广视图,也就是集合视图的头视图或者尾视图- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { MyCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath]; view.backgroundColor = [UIColor blueColor]; view.headerLabel.text = [NSString stringWithFormat:@"仙逆第%ld部 ",indexPath.section + 1]; return view; }#pragma mark -----集合视图代理方法----//Item点击之后触发的方法- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { NSLog(@"%ld %ld",indexPath.section,indexPath.row);}#import "MyCollectionViewCell.h"@implementation MyCollectionViewCell- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //获取整体Item的宽和高 CGFloat totalWidth = frame.size.width; CGFloat totalHeight = frame.size.height; self.numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, totalWidth, totalHeight - 20)]; self.numberLabel.textAlignment = NSTextAlignmentCenter; self.numberLabel.font = [UIFont boldSystemFontOfSize:14]; [self.contentView addSubview:self.numberLabel]; } return self;}@end#import "MyCollectionReusableView.h"@implementation MyCollectionReusableView- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.headerLabel = [[UILabel alloc] initWithFrame:self.bounds]; self.headerLabel.textAlignment = NSTextAlignmentCenter; self.headerLabel.font = [UIFont boldSystemFontOfSize:36]; [self addSubview:self.headerLabel]; } return self;}@end