//通過一個布局策略初識化CollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;
//擷取和設定collection的layout
@property (nonatomic, strong) UICollectionViewLayout *collectionViewLayout;
//資料來源和代理
@property (nonatomic, weak, nullable) id <UICollectionViewDelegate> delegate;
@property (nonatomic, weak, nullable) id <UICollectionViewDataSource> dataSource;
//從一個class或者xib檔案進行cell(item)的註冊
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath//一點擊item執行
{
NSLog(@"-----------1------1----");
}
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath//點擊鬆開手後執行
{
NSLog(@"-----------2------2----");
}
//下面兩個方法與上面相似,這裡註冊的是頭視圖或者尾視圖的類
//其中第二個參數是設定 頭視圖或者尾視圖 系統為我們定義好了這兩個字串
//UIKIT_EXTERN NSString *const UICollectionElementKindSectionHeader NS_AVAILABLE_IOS(6_0);
//UIKIT_EXTERN NSString *const UICollectionElementKindSectionFooter NS_AVAILABLE_IOS(6_0);
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;
//這兩個方法是從複用池中取出cell或者頭尾視圖
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
//設定是否允許選中 預設yes
@property (nonatomic) BOOL allowsSelection;
//設定是否允許多選 預設no
@property (nonatomic) BOOL allowsMultipleSelection;
//擷取所有選中的item的位置資訊
- (nullable NSArray<NSIndexPath *> *)indexPathsForSelectedItems;
//設定選中某一item,並使視圖滑動到相應位置,scrollPosition是滑動位置的相關參數,如下:
/*
typedef NS_OPTIONS(NSUInteger, UICollectionViewScrollPosition) {
//無
UICollectionViewScrollPositionNone = 0,
//垂直布局時使用的 對應上中下
UICollectionViewScrollPositionTop = 1 << 0,
UICollectionViewScrollPositionCenteredVertically = 1 << 1,
UICollectionViewScrollPositionBottom = 1 << 2,
//水平布局時使用的 對應左中右
UICollectionViewScrollPositionLeft = 1 << 3,
UICollectionViewScrollPositionCenteredHorizontally = 1 << 4,
UICollectionViewScrollPositionRight = 1 << 5
};
*/
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
//將某一item取消選中
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
//重新載入資料
- (void)reloadData;
//下面這兩個方法,可以重新設定collection的布局,後面的方法多了一個布局完成後的回調,iOS7後可以用
//使用這兩個方法可以產生非常炫酷的動畫效果
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated;
- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(7_0);
//下面這些方法更加強大,我們可以對布局更改後的動畫進行設定
//這個方法傳入一個布局策略layout,系統會開始進行布局渲染,返回一個UICollectionViewTransitionLayout對象
//這個UICollectionViewTransitionLayout對象管理動畫的相關屬性,我們可以進行設定
- (UICollectionViewTransitionLayout *)startInteractiveTransitionToCollectionViewLayout:(UICollectionViewLayout *)layout completion:(nullable UICollectionViewLayoutInteractiveTransitionCompletion)completion NS_AVAILABLE_IOS(7_0);
//準備好動畫設定後,我們需要調用下面的方法進行布局動畫的展示,之後會調用上面方法的block回調
- (void)finishInteractiveTransition NS_AVAILABLE_IOS(7_0);
//調用這個方法取消上面的布局動畫設定,之後也會進行上面方法的block回調
- (void)cancelInteractiveTransition NS_AVAILABLE_IOS(7_0);
//擷取分區數
- (NSInteger)numberOfSections;
//擷取某一分區的item數
- (NSInteger)numberOfItemsInSection:(NSInteger)section;
//下面兩個方法擷取item或者頭尾視圖的layout屬性,這個UICollectionViewLayoutAttributes對象
//存放著布局的相關資料,可以用來做完全自訂布局,後面部落格會介紹
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;
//擷取某一點所在的indexpath位置
- (nullable NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point;
//擷取某個cell所在的indexPath
- (nullable NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell;
//根據indexPath擷取cell
- (nullable UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;
//擷取所有可見cell的數組
- (NSArray<__kindof UICollectionViewCell *> *)visibleCells;
//擷取所有可見cell的位置數組
- (NSArray<NSIndexPath *> *)indexPathsForVisibleItems;
//下面三個方法是iOS9中新添加的方法,用於擷取頭尾視圖
- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
- (NSArray<UICollectionReusableView *> *)visibleSupplementaryViewsOfKind:(NSString *)elementKind NS_AVAILABLE_IOS(9_0);
- (NSArray<NSIndexPath *> *)indexPathsForVisibleSupplementaryElementsOfKind:(NSString *)elementKind NS_AVAILABLE_IOS(9_0);
//使視圖滑動到某一位置,可以帶動畫效果
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
//下面這些方法用於動態添加,刪除,移動某些分區擷取items
- (void)insertSections:(NSIndexSet *)sections;
- (void)deleteSections:(NSIndexSet *)sections;
- (void)reloadSections:(NSIndexSet *)sections;
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;
- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;