//UICollectionViewLayout 布局類的父類
//UICollectionViewFlowLayout 系統(tǒng)自帶布局類
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//設(shè)置每個Item的大小
//collectionView中的列數(shù)冤今,不是確定辛慰,間隔也不是確定
flowLayout.itemSize = CGSizeMake(80, 80);
//列最低間隔
flowLayout.minimumInteritemSpacing = 20;
//行最低間隔
flowLayout.minimumLineSpacing = 20;
//分區(qū)內(nèi)所有item區(qū)域距離邊框和分區(qū)頭的寬度
//上、左赋咽、下角溃、右逆時針的順序
flowLayout.sectionInset = UIEdgeInsetsMake(50, 10, 100, 10);
//設(shè)置滾動方向
flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
//創(chuàng)建UICollectionView
UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[self getWaterFallLayout]];
//UICollectionView 嚴(yán)格遵守MVC模式
collectionView.dataSource = self;
collectionView.delegate = self;
//注冊CollectionViewCell
[collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"reuse”];
//注冊分區(qū)頭
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView”];
//注冊分區(qū)尾巴
[collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView”];
#pragma mark UICollectionView的協(xié)議方法
#pragma mark 設(shè)置分區(qū)個數(shù)
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
#pragma mark 設(shè)置分區(qū)頭和尾
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//collectionView的分區(qū)頭和分尾都遵從重用機(jī)制
//判斷類型為頭或為尾
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];
headerView.backgroundColor = [UIColor grayColor];
return headerView;
}
else
{
//通過重用標(biāo)識和類型獲取對應(yīng)的重用view
UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];
footerView.backgroundColor = [UIColor yellowColor];
return footerView;
}
}
#pragma mark 返回分區(qū)有多少個Item (默認(rèn)是有一個分區(qū))
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.dataArray.count;
}
#pragma mark 點擊方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
#pragma mark UICollectionViewLayout 的協(xié)議方法
#pragma makr 設(shè)置分區(qū)頭的size
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
//橫向滾動時,高度無效
//縱向滾動時,寬度無效
return CGSizeMake(20, 100);
}
//item在顯示之前會調(diào)動這個方法布局
//計算所有Item的布局
-(void)prepareLayout
//返回整個布局的contentSize(可滾動區(qū)域)
-(CGSize)collectionViewContentSize