UICollecionView的創(chuàng)建和屬性使用

//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 嚴格遵守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ū)頭和分尾都遵從重用機制

//判斷類型為頭或為尾

if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {

UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView" forIndexPath:indexPath];

headerView.backgroundColor = [UIColor grayColor];

return headerView;

}

else

{

//通過重用標識和類型獲取對應(yīng)的重用view

UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footerView" forIndexPath:indexPath];

footerView.backgroundColor = [UIColor yellowColor];

return footerView;

}

}

#pragma mark 返回分區(qū)有多少個Item (默認是有一個分區(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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容