UICollectionViewFlowLayout類是一個(gè)具體的布局對(duì)象娱颊,它將一個(gè)個(gè)部件組織成一個(gè)可分組的(每個(gè)分組都有可選的頁眉和頁腳視圖)網(wǎng)格铆农;這些部件在集合視圖中從一行流向下一行或者一列流向下一列(根據(jù)滾動(dòng)方向),每個(gè)單元都可以是相同的尺寸或者不同的尺寸;
一個(gè)流布局是通過集合視圖的委托來決定每個(gè)分組的部件酪呻,頭部以及底部的尺寸大小的,這個(gè)委托對(duì)象必須執(zhí)行UICollectionViewDelegateFlowLayout協(xié)議盐须,通過這個(gè)協(xié)議可以讓你動(dòng)態(tài)的適應(yīng)布局信息玩荠,如果你沒有提供一個(gè)委托對(duì)象,那流布局將會(huì)使用你所設(shè)置的本類的以下屬性的默認(rèn)值贼邓。
流布局在一個(gè)方向上是用一個(gè)固定的距離來布局他們的內(nèi)容視圖阶冈,而在其他方向上是一個(gè)可滾動(dòng)的距離,例如塑径,在一個(gè)垂直滾動(dòng)網(wǎng)格女坑,當(dāng)內(nèi)容視圖的高度動(dòng)態(tài)適應(yīng)網(wǎng)格中的分組和單元格的數(shù)目 網(wǎng)格的內(nèi)容視圖的寬度將被約束到相應(yīng)的集合視圖的寬度。布局配置為默認(rèn)垂直滾動(dòng)统舀,你也可以使用scrolldirection屬性來配置的滾動(dòng)方向匆骗。
流布局的每一個(gè)分組都有它自己的頭部和底部,如果你想獲取頭部和底部視圖誉简,你必須配置頭部和底部的尺寸為非零值碉就,你可以通過實(shí)現(xiàn)相應(yīng)地委托方法來配置,或者你也可以通過設(shè)置相應(yīng)地屬性值來配置(headerReferenceSize 和 footerReferenceSize)闷串,如果尺寸為0瓮钥,那么相應(yīng)的頭部和底部視圖將不會(huì)被添加到集合視圖中去。
//列間距
@property (nonatomic) CGFloat minimumLineSpacing;
// 行間距
@property (nonatomic) CGFloat minimumInteritemSpacing;
//cell尺寸
@property (nonatomic) CGSize itemSize;
//預(yù)計(jì)cell尺寸
@property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -perferredLayoutAttributesFittingAttributes:
//滾動(dòng)方向(水平或者垂直)
@property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
//頭部尺寸
@property (nonatomic) CGSize headerReferenceSize;
//底部尺寸
@property (nonatomic) CGSize footerReferenceSize;
//組的邊緣尺寸
@property (nonatomic) UIEdgeInsets sectionInset;
UICollectionViewDelegateFlowLayout協(xié)議
//每個(gè)cell的尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
//每個(gè)分組的邊緣尺寸
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
/每個(gè)分組的/列間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;
//每個(gè)分組的行間距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
//每個(gè)分組的頭部尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;
//每個(gè)分組的底部尺寸
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;
創(chuàng)建UICollectionView烹吵,通過- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;方法來創(chuàng)建集合視圖碉熄,layout不能為空;
UICollectionViewFlowLayout*flowLayout?=?[[UICollectionViewFlowLayoutalloc]init];
flowLayout.scrollDirection=?UICollectionViewScrollDirectionVertical;
//??????設(shè)置headview
flowLayout.headerReferenceSize=?CGSizeMake(self.frame.size.width,30);
CGRect?rect?=self.bounds;
rect.origin.x=?rect.size.width*?count;
UICollectionView*collectionView?=?[[UICollectionViewalloc]initWithFrame:rectcollectionViewLayout:flowLayout];
collectionView.backgroundColor=?[UIColorwhiteColor];
[collectionViewregisterClass:[LaunchCollectionViewCellclass]forCellWithReuseIdentifier:CellIdentifier];
//??????設(shè)置headview
[collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ReusableView"];
collectionView.showsHorizontalScrollIndicator=NO;
collectionView.showsVerticalScrollIndicator=NO;
collectionView.pagingEnabled=NO;
collectionView.scrollEnabled=NO;
collectionView.dataSource=self;
collectionView.delegate=self;
[scrollViewaddSubview:collectionView];
和UITableView類似年叮,實(shí)現(xiàn)兩個(gè)協(xié)議UICollectionViewDelegate和UICollectionViewDataSource
UICollectionViewDataSource
//每組的單元格數(shù)量
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
//創(chuàng)建單元格視圖具被,
//這里的cell必須通過 -dequeueReusableCellWithReuseIdentifier:forIndexPath:方法來檢索可重用的單元格,且可重用標(biāo)示符必須和創(chuàng)建UICollectionView時(shí)通過- registerClass:forCellWithReuseIdentifier:或者registerNib: forCellWithReuseIdentifier:方法注冊(cè)的標(biāo)示符要一致
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//分組數(shù)
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;
//創(chuàng)建頭部或者底部視圖
// 這里的視圖必須通過 -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:方法來檢索可重用的視圖只损,且可重用標(biāo)示符必須和創(chuàng)建UICollectionView時(shí)通過registerClass: forSupplementaryViewOfKind:withReuseIdentifier:或者registerNib: forSupplementaryViewOfKind: withReuseIdentifier:方法注冊(cè)的標(biāo)示符要一致
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
UICollectionViewDelegate
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;
// These methods provide support for copy/paste actions on cells.
// All three should be implemented if any are.
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender;
// support for custom transition layout
- (UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout;
此外UICollectionView還能通過以下方法實(shí)現(xiàn)簡(jiǎn)單的修改
//滾動(dòng)到某分組的某單元
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
// These methods allow dynamic modification of the current set of items in the collection view
//插入某些組
- (void)insertSections:(NSIndexSet *)sections;
//刪除某些組
- (void)deleteSections:(NSIndexSet *)sections;
//重新加載某些組
- (void)reloadSections:(NSIndexSet *)sections;
//移動(dòng)組
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
//添加一些單元格
- (void)insertItemsAtIndexPaths:(NSArray *)indexPaths;
//刪除一些單元格
- (void)deleteItemsAtIndexPaths:(NSArray *)indexPaths;
//創(chuàng)新加載一些單元格
- (void)reloadItemsAtIndexPaths:(NSArray *)indexPaths;
//移動(dòng)單元格
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;