@interface ViewController () <UICollectionViewDataSource,UICollectionViewDelegate>
// 對(duì)其操作遵循的兩個(gè)協(xié)議
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//第一步:創(chuàng)建UICollectionViewFlowLayout 它是專(zhuān)門(mén)給collectionView布局
UICollectionViewFlowLayout *flowLayout =[UICollectionViewFlowLayout new];
//第二步: 給定item的大小
flowLayout.itemSize=CGSizeMake(60, 60);
//第三步: 設(shè)定每?jī)蓚€(gè)item的最小的間隔(垂直滾動(dòng))
flowLayout.minimumInteritemSpacing=10;
//每行之間的最小間隔 (水平滾動(dòng))
flowLayout.minimumLineSpacing=40;
//第四步: 設(shè)置滾動(dòng)的方向 (垂直)
flowLayout.scrollDirection=UICollectionViewScrollDirectionVertical;
//布局頭部視圖尺寸
flowLayout.headerReferenceSize=CGSizeMake(80, 80);
//布局尾部視圖尺寸
flowLayout.footerReferenceSize=CGSizeMake(80, 80);
//視圖的內(nèi)邊距
flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//布局UICollectionView
UICollectionView *collection = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
//設(shè)置代理
collection.delegate=self;
collection.dataSource=self;
collection.backgroundColor=[UIColor redColor];
[self.view addSubview:collection];
//注冊(cè)cell
[collection registerClass:[FirstCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
//注冊(cè)頭部視圖
[collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
//注冊(cè)尾部視圖
[collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView"];
}
#pragma mark -------數(shù)據(jù)源 (必須實(shí)現(xiàn)的方法 )
//設(shè)置分區(qū)下item數(shù)
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 20;
}
//返回collectionView的方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
FirstCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.photoImage.image = [UIImage imageNamed:@"3.jpg"];
return cell;
}
//@optional
//設(shè)置分區(qū)數(shù)
-(NSInteger )numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 4;
}
//返回頭部和尾部視圖的樣式
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//需要判斷返回的是頭部還是尾部視圖
if ([kind isEqualToString:UICollectionElementKindSectionHeader])
{
//初始化頭部視圖
UICollectionReusableView *headerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
//設(shè)置頭部視圖樣式
headerView.backgroundColor=[UIColor cyanColor];
return headerView;
}else
{
UICollectionReusableView *footerView=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor greenColor];
return footerView;
}
return nil;
}
//選擇item時(shí) ,會(huì)觸發(fā)的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//indexPath 是由 section和item 組成
if (indexPath.item == 0 && indexPath.section == 0) {
SecondViewController *secondVC=[SecondViewController new];
點(diǎn)擊第一區(qū)第一個(gè)單元cell跳轉(zhuǎn)到 secondVC
[self.navigationController pushViewController:secondVC animated:YES];
}else if (indexPath.item == 1 && indexPath.section == 0)
{
ThirdViewController *thirdVC=[ThirdViewController new];
點(diǎn)擊第一區(qū)第二個(gè)單元cell跳轉(zhuǎn)到 thirdVC
[self.navigationController pushViewController:thirdVC animated:YES];
}
}