標籤:
IOS---UICOLLECTIONVIEW詳解和常用API翻譯UICollectionView
用法類似於UITableView 類。自動實現重用,必須註冊初始化。使用UICollectionView必須實現
UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout這三個協議。Collection View的構成,我們能看到的有三個部分:
- Cells
- Supplementary Views 追加視圖 (類似Header或者Footer)
- Decoration Views 裝飾視圖 (用作背景展示)
一、UICollectionViewLayout
- UICollectionView的精髓
- Layout決定了UICollectionView是如何顯示在介面上的。在展示之前,一般需要產生合適的UICollectionViewLayout子類對象,並將其賦予CollectionView的collectionViewLayout屬性。
1.UICollectionViewFlowLayout
- 最簡單也是最常用的預設layout對象,???UICollectionViewFlowLayout。Flow Layout簡單說是一個直線對齊的layout,
常用屬性
// 行間距,也可以通過collectionView: layout:minimumLineSpacingForSectionAtIndex:@property (nonatomic) CGFloat minimumLineSpacing;// 設定cell之間的間距@property (nonatomic) CGFloat minimumInteritemSpacing;// 定義每一個item的大小。通過設定itemSize可以全域地改變所有cell的尺寸,如果想要對某個cell制定尺寸,//可以使用-collectionView:layout:sizeForItemAtIndexPath:方法。@property (nonatomic) CGSize itemSize;@property (nonatomic) CGSize estimatedItemSize// 滾動方向,預設是水平// UICollectionViewScrollDirectionVertical// UICollectionViewScrollDirectionHorizontal@property (nonatomic) UICollectionViewScrollDirection scrollDirection;// 根據滾動方向不同,header和footer的高和寬中只有一個會起作用。// 垂直滾動時section間寬度為該尺寸的高,而水平滾動時為寬度起作用,@property (nonatomic) CGSize headerReferenceSize;@property (nonatomic) CGSize footerReferenceSize;// 組間距 縮排@property (nonatomic) UIEdgeInsets sectionInset;
UICollectionViewDelegateFlowLayout代理方法
// 設定指定Cell的尺寸- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;// 設定collectionView(指定區)的邊距- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;// 設定指定區內Cell的最小行距,也可以直接設定UICollectionViewFlowLayout的minimumLineSpacing屬性- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section// 設定指定區內Cell的最小間距,也可以直接設定UICollectionViewFlowLayout的minimumInteritemSpacing屬性- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
2.UICollectionViewLayoutAttributes
屬性列表
// 邊框@property (nonatomic) CGRect frame// 中心點@property (nonatomic) CGPoint center// 大小@property (nonatomic) CGSize size// 形狀@property (nonatomic) CATransform3D transform3D// 透明度@property (nonatomic) CGFloat alpha// 層次關係@property (nonatomic) NSInteger zIndex// 是否隱藏@property (nonatomic, getter=isHidden) BOOL hidden
3.自訂的UICollectionViewLayout
- UICollectionViewLayout的功能為向UICollectionView提供布局資訊.
- 繼承UICollectionViewLayout類。
重寫方法
// 返回collectionView的內容的尺寸-(CGSize)collectionViewContentSize// 返回rect中的所有的元素的布局屬性/*返回的是包含UICollectionViewLayoutAttributes的NSArrayUICollectionViewLayoutAttributes可以是cell,追加視圖或裝飾視圖的資訊,通過不同的UICollectionViewLayoutAttributes初始化方法可以得到不同類型的UICollectionViewLayoutAttributes:*/-(NSArray *)layoutAttributesForElementsInRect:(CGRect)rect// 返回對應於indexPath的位置的cell的布局屬性-(UICollectionViewLayoutAttributes _)layoutAttributesForItemAtIndexPath:(NSIndexPath _)indexPath//返回對應於indexPath的位置的追加視圖的布局屬性,如果沒有追加視圖可不重載-(UICollectionViewLayoutAttributes _)layoutAttributesForSupplementaryViewOfKind:(NSString _)kind atIndexPath:(NSIndexPath *)indexPath// 返回對應於indexPath的位置的裝飾視圖的布局屬性,如果沒有裝飾視圖可不重載-(UICollectionViewLayoutAttributes * )layoutAttributesForDecorationViewOfKind:(NSString_)decorationViewKind atIndexPath:(NSIndexPath _)indexPath// 當邊界發生改變時,是否應該重新整理布局。如果YES則在邊界變化(一般是scroll到其他地方)時,將重新計算需要的布局資訊。-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
二、UICollectionView1.UICollectionViewDataSource①section的數量 ?-numberOfSectionsInCollection:②某個section裡有多少個item ?-collectionView:numberOfItemsInSection:③對於某個位置應該顯示什麼樣的cell ?-collectionView:cellForItemAtIndexPath:2.UICollectionViewDelegate
// 當指定indexPath處的item被選擇時觸發- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath//返回這個UICollectionView是否可以被選擇-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;// 下面是三個和高亮有關的方法:- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath/*事件的處理順序如下:1.手指按下2.shouldHighlightItemAtIndexPath (如果返回YES則向下執行,否則執行到這裡為止)3.didHighlightItemAtIndexPath (高亮)4.手指鬆開5.didUnhighlightItemAtIndexPath (取消高亮)6.shouldSelectItemAtIndexPath (如果返回YES則向下執行,否則執行到這裡為止)7.didSelectItemAtIndexPath (執行選擇事件)
IOS---UICOLLECTIONVIEW詳解和常用API翻譯