UICollectionView介紹
UICollectionView類負(fù)責(zé)管理數(shù)據(jù)的有序集合以及自定義布局的模式來呈現(xiàn)數(shù)據(jù),提供一些常用表格功能,此外還增加了單欄布局。也支持可以用于實現(xiàn)多列網(wǎng)格、平鋪的布局、圓形的布局和更多的自定義布局,甚至你可以動態(tài)改變它的布局。
UICollectionView代碼創(chuàng)建方式
UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounts collectionViewLayout:layout];
collectionView.delegate = self;
collectionView.dataSource = self;
[collectionView registerClass:[xxxCell class] forCellWithReuseIdentifier:@"cell"];
1.這個layout是什么呢?
UICollecitonViewLayout-是UICollecitonView特有的,負(fù)責(zé)其組成部分的cell,supplementary view和decoration view的組合。這三個view在后篇文章中會介紹。其能為三者設(shè)置各自的屬性,包括:位置、大小、透明度、層級關(guān)系、形狀等(存儲在UICollectionViewLayoutAttributes,每個cell對應(yīng)一個屬于自己的UICollectionViewLayoutAttributes)。UICollectionView初始化都需要layout布局,可見其重要性。但是UICollectionViewLayout是一個抽象類,只定義了一些子類公有的屬性和行為,不能直接使用。如果使用的話,則需要自己寫布局。
UICollectionViewFlowLayout流式布局
UICollectionViewFlowLayout流式布局,蘋果為我們設(shè)計非常靈活、通用的Layout。意思是UI控件會像流水一樣,一行排滿自動下一行排,這些布局都是蘋果寫好的。
UICollectionViewFlowLayout屬性:
CGSize itemSize:定義所有的itemSize可以快捷給cell設(shè)置一樣的大?。ㄈ绻鹀ell大小需求一樣則可以用這個設(shè)置)。
CGFloat minimumLineSpacing:最小行間距
CGFloat minmumInteritemSpacing:最小cell之間的間距
UIEdgeInsets sectionInset:組內(nèi)邊距,設(shè)置的是UICollectionView整體的組內(nèi)邊距
CGSize headerReferenceSize:設(shè)置supplementary header View的大小
CGSize footerReferenceSize:設(shè)置supplementary header View的大小
UICollectionViewScrollDirection scrollDirection:設(shè)置UICollectionView的滾動放向
BOOL sectionHeadersPinToVisibleBounds:設(shè)置是否當(dāng)元素超出屏幕之后固定頭部試圖位置,默認(rèn)NO
BOOL sectionFootersPinToVisibleBounds:設(shè)置是否當(dāng)元素超出屏幕之后固定尾部試圖位置,默認(rèn)NO
以上都是通過UICollectionViewFlowLayout來設(shè)置全局的布局,如果需要設(shè)置單獨(dú)的每個item的屬性則需要用UICollectionViewDelegateFlowLayout來設(shè)置。
UICollectionViewDelegateFlowLayout
//單獨(dú)定制item的尺寸
- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//定義每個UICollectionView的margin(間距),對每一個section單獨(dú)設(shè)置邊界,即內(nèi)部cell上下左右距離header和footer的邊界(間距)
- (UIEdgeInsets)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
//單獨(dú)定制每行之間的間距
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
//單獨(dú)定制每行item之間的間距
- (CGFloat)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//單獨(dú)定制頭部視圖size
- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//單獨(dú)定制腳注視圖size
- (CGSize)collectionView:(UICollectionView )collectionView layout:(UICollectionViewLayout)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
UICollectionViewDataSource
UICollectionViewDataSource跟UITableViewDataSource類似。
//設(shè)置section組數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//設(shè)置item個數(shù)
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//配置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//自定義頭部、腳注視圖
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
UIScrollViewDelegate
UIScrollViewDelegate也和UITableViewDelegate類似功能,但有獨(dú)特的代理方法
//設(shè)置是否允許選中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//設(shè)置是否允許取消選中
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
//選中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
//取消選中
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
一定要注冊cell不然會爆這樣的錯誤
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier myCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
初步使用這些,就能簡單的使用collectionView
轉(zhuǎn)載: http://www.itdecent.cn/p/1044873c20c9