@interface XCZCollectionHeader : UICollectionReusableView佈局
#import "XCZCollectionsViewController.h"spa
#import "XCZCollectionViewCell.h".net
#import "XCZCollectionHeader.h"代理
@interface XCZCollectionsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>orm
@end 對象
@implementation XCZCollectionsViewControllerip
- (void)viewDidLoad {ci
[super viewDidLoad];get
// Do any additional setup after loading the view.it
self.title = @"分類";
[self createCollectionView];
}
- (void)createCollectionView {
// 第一步:新建佈局對象
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
CGFloat padding = 10;
CGFloat itemW = (self.view.frame.size.width - 5 * padding)/4;
CGFloat itemH = itemW;
layout.itemSize = CGSizeMake(itemW, itemH);
// 頭尾高度
layout.headerReferenceSize = CGSizeMake(0, 10);
// 最小間隔
// 對於垂直滾動,它是行間距
// 對於水平滾動,它是列間距
layout.minimumLineSpacing = 10;
// 內邊距
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
// 集合視圖,用於塊狀分佈
// iOS6.0以後出現的視圖
// 第二步,新建集合視圖,關聯佈局對象
[self setAutomaticallyAdjustsScrollViewInsets:NO];
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, [[UIScreen mainScreen] bounds].size.height - 64) collectionViewLayout:layout];
// 第三步,註冊cell, 註冊追加視圖(section的頭和尾)
// 註冊cell
[collectionView registerClass:[XCZCollectionViewCell class] forCellWithReuseIdentifier:@"MyCellID"]; // 複用標識,可隨意寫
// 註冊supplementary view
// 第一個參數:註冊的類
// 第二個參數:追加視圖的類型 UICollectionElementKindSectionHeader 表示頭,UICollectionElementKindSectionFooter表示尾 至關於tableView 中分區的headerView和footerView
// 第二個參數:複用標識
[collectionView registerClass:[XCZCollectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MySectionHeaderViewID"]; // 註冊頭
// 第四步,設置代理,並實現代理方法
collectionView.delegate = self;
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:collectionView];
}
#pragma mark - 實現代理方法
// 多少個分區
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 4;
}
// 每一個分區有多少cell(item)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0) {
return 7;
}else if (section == 1){
return 11;
}else if (section == 2){
return 12;
}else if (section == 3){
return 7;
}
return section;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 每一個cell是什麼
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XCZCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MyCellID" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"1"];
cell.textLabel.text = @"嘎嘎";
return cell;
}
// 頭尾視圖
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
// 根據是頭視圖仍是尾視圖做判斷
XCZCollectionHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"MySectionHeaderViewID" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor grayColor];
NSArray * arr = @[@"選集",@"主題",@"寫景",@"節日"];
// NSString *title = [[NSString alloc] initWithFormat:@"Recipe Group #%li",indexPath.section +1];
headerView.textLabel.text = arr[indexPath.section];
return headerView;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"--%ld",(long)indexPath.row);
}
// 經過代理方法設置headerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
return CGSizeMake(0, 26);
}