您的位置:首页 > 产品设计 > UI/UE

UICollectionView.h文件

2016-04-07 20:35 85 查看
#import <UIKit/UIScrollView.h>
#import <UIKit/UIKitDefines.h>
#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

// collectionViewCell滚动的位置
typedef NS_OPTIONS(NSUInteger, UICollectionViewScrollPosition) {
UICollectionViewScrollPositionNone =
0,

// The vertical positions are mutually exclusive to each other, but are bitwise or-able with the horizontal scroll positions.
// Combining positions from the same grouping (horizontal or vertical) will result in an NSInvalidArgumentException.
/** 垂直方向 */
// 滚动到头部
UICollectionViewScrollPositionTop =
1 << 0,
// 滚动到垂直居中
UICollectionViewScrollPositionCenteredVertically =
1 << 1,
// 滚动到底部
UICollectionViewScrollPositionBottom =
1 << 2,

// Likewise, the horizontal positions are mutually exclusive to each other.
/** 水平方法 */
// 左边
UICollectionViewScrollPositionLeft =
1 << 3,
// 水平中心
UICollectionViewScrollPositionCenteredHorizontally =
1 << 4,
// 右边
UICollectionViewScrollPositionRight =
1 << 5
};

@class UICollectionView;
@class UICollectionViewCell;
@class UICollectionViewLayout;
@class UICollectionViewTransitionLayout;
@class UICollectionViewLayoutAttributes;
@class UITouch;
@class UINib;
@class UICollectionReusableView;

// layout transition block signature
typedef void (^UICollectionViewLayoutInteractiveTransitionCompletion)(BOOL completed,
BOOL finished);
#ifndef SDK_HIDE_TIDE
// collectionView焦点更新内容
// 提供相关的从一个视图到另一个特别关注更新信息。他们是更新完成后,通常是短暂的丢弃的对象。
NS_CLASS_AVAILABLE_IOS(9_0)
@interface UICollectionViewFocusUpdateContext : UIFocusUpdateContext
// 上一个交互的索引
@property (nonatomic,
strong, readonly,
nullable) NSIndexPath *previouslyFocusedIndexPath;
// 下一个交互的索引
@property (nonatomic,
strong, readonly,
nullable) NSIndexPath *nextFocusedIndexPath;

@end
#endif

#pragma mark - UICollectionView的数据源协议
@protocol UICollectionViewDataSource <NSObject>
@required
// 返回每一组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
/**
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
必须提前注册cell并设置重用标识
注册cell的三种方式:
1> 用nib(xib)来注册cell,表示cell如何去创建,
在注册同时必须给cell设置重用标识
2> 用类(纯代码)来注册cell,表示cell用代码来创建,在注册同时必须cell设置重用标识
3> 在storyboard中给cell,设置重用标识时会同时注册cell
// 苹果强烈建议我们利用cell的循环利用机制
*/
// 返回每一组的每一个cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;

@optional
// 返回有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;

// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
// 返回collectionView的 haderView和footerView,也必须提前注册
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

// 移动cell
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 移动cell到什么位置
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);

@end

#pragma mark - UICollectionVoiew的代理
@protocol UICollectionViewDelegate <UIScrollViewDelegate>
@optional

// Methods for notification of selection/deselection and highlight/unhighlight events.
// The sequence of calls leading to selection from a user touch is:
//
// (when the touch begins)当触摸开始
// 1. -collectionView:shouldHighlightItemAtIndexPath:
// 2. -collectionView:didHighlightItemAtIndexPath:
//
// (when the touch lifts)当触摸抬起
// 3. -collectionView:shouldSelectItemAtIndexPath: or -collectionView:shouldDeselectItemAtIndexPath:
// 4. -collectionView:didSelectItemAtIndexPath: or -collectionView:didDeselectItemAtIndexPath:
// 5. -collectionView:didUnhighlightItemAtIndexPath:

/** cell将要高亮 */
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;

// 选中某个cell
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;

#warning mark - 如果设置cell允许多选时不会调用取消选中
- (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 didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;

// cell将要显示
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(8_0);
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

// headerView/FooterView将要显示
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath
*)indexPath NS_AVAILABLE_IOS(8_0);
- (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.
// 复制粘帖cell
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath;
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath
withSender:(nullable id)sender;
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable
id)sender;

// support for custom transition layout
// 自定义转换布局
- (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout
*)toLayout;
#ifndef SDK_HIDE_TIDE
// Focus 焦点
- (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
- (BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context NS_AVAILABLE_IOS(9_0);
- (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator
*)coordinator NS_AVAILABLE_IOS(9_0);
- (nullable NSIndexPath *)indexPathForPreferredFocusedViewInCollectionView:(UICollectionView *)collectionView NS_AVAILABLE_IOS(9_0);
#endif

- (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath NS_AVAILABLE_IOS(9_0);

- (CGPoint)collectionView:(UICollectionView *)collectionView targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset NS_AVAILABLE_IOS(9_0);
// customize the content offset to be applied during transition or update animations

@end

NS_CLASS_AVAILABLE_IOS(6_0)
@interface UICollectionView : UIScrollView
// 实例化UICollectionView时一定要指定一个布局对象,不能为空
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout NS_DESIGNATED_INITIALIZER;
// 在IB中创建UICollectionView时会调用此方法
- (nullable
instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

/** collectionView的布局 */
@property (nonatomic,
strong) UICollectionViewLayout *collectionViewLayout;
@property (nonatomic,
weak, nullable)
id <UICollectionViewDelegate> delegate;
// 代理
@property (nonatomic,
weak, nullable)
id <UICollectionViewDataSource> dataSource;
// 数据源
// 背景视图,会自动填充整个collectionView
@property (nonatomic,
strong, nullable) UIView *backgroundView;

// 通过类来"注册"创建cell,并给cell设置重用标识
- (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;
// 通过xib来"注册"创建cell,并给cell设置重用标识
- (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;

// 通过类来"注册"创建附加视图(HeaderView/FooterView),并给cell设置重用标识
- (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;
// 通过xib来"注册"创建附加视图(HeaderView/FooterView),并给cell设置重用标识
- (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;

/**
* 用返回可重用的cell,在此之前必须提前注册cell(指明cell如果创建)并给cell设置重用标识
*
* @param identifier cell的重用标识符,此参数不能是nil
* @param indexPath 索引路径指定的cell的位置,该方法使用该索引的路径来进行基于在集合视图中的单元格的位置的附加配置
*/
- (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;
/**
* 用返回可重用的Header/Footer,在此之前必须提前注册Header/Footer(指明Header/Footer如果创建)并给Header/Footer设置重用标识
*
* @param identifier Header/Footer的重用标识,此参数不能是nil
* @param indexPath 索引路径指定的Header/Footer的位置,该方法使用该索引的路径来进行基于在集合视图中的单元格的位置的附加配置
*/
- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;

/** 是否允许选中cell
默认允许选中*/
@property (nonatomic)
BOOL allowsSelection; // default is YES
/** 是否可以多选
默认只是单选 */
@property (nonatomic)
BOOL allowsMultipleSelection;
// default is NO

/** 返回选中cell的索引 */
- (nullable NSArray<NSIndexPath *> *)indexPathsForSelectedItems;

// 选中cell,并滚动到选中cell的什么位置
- (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition;
// 取消选中
- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;
/** 重新刷新表格 (弃用之前所有cell,重新调用数据源方法) */
- (void)reloadData;

// 设置collectionView的布局
- (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);

- (UICollectionViewTransitionLayout *)startInteractiveTransitionToCollectionViewLayout:(UICollectionViewLayout *)layout completion:(nullable UICollectionViewLayoutInteractiveTransitionCompletion)completion
NS_AVAILABLE_IOS(7_0);
- (void)finishInteractiveTransition NS_AVAILABLE_IOS(7_0);
- (void)cancelInteractiveTransition NS_AVAILABLE_IOS(7_0);

// Information about the current state of the collection view.

// 返回有多少组
- (NSInteger)numberOfSections;
// 返回section组有多少个cell
- (NSInteger)numberOfItemsInSection:(NSInteger)section;

// 返回指定cell的布局属性
- (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;
// 返回指定Header/Footer的布局属性
- (nullable UICollectionViewLayoutAttributes *)
layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;

// 返回指定位置的cell索引
- (nullable NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point;
// 返回cell的索引
- (nullable NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell;
// 返回指定索引的cell
- (nullable UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;
// 返回可视范围内的所有cell
- (NSArray<__kindof UICollectionViewCell *> *)visibleCells;
// 返回可视范围内所有cell的索引
- (NSArray<NSIndexPath *> *)indexPathsForVisibleItems;

// 返回指定索引的Header/Footer
- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// 返回可视范围内的所有Header/Footer
- (NSArray<UICollectionReusableView *> *)visibleSupplementaryViewsOfKind:(NSString *)elementKind NS_AVAILABLE_IOS(9_0);
// 返回可视范围内所有Header/Footer的索引
- (NSArray<NSIndexPath *> *)indexPathsForVisibleSupplementaryElementsOfKind:(NSString *)elementKind NS_AVAILABLE_IOS(9_0);

// Interacting with the collection view.//
与集合视图进行交互
// 滚动到指定索引的cell的某个位置
- (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;
// 移动某组到那一组
- (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;

// 在指定位置插入单个或多个cell
- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
// 删单个或多个位置的cell
- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
// 刷新单个或多个的cell
- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths;
// 移动某个cell到什么位置
- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;

// 执行多个插入/删除/恢复/移动 cell可以同时动画。可嵌套的。
- (void)performBatchUpdates:(void (^
__nullable)(void))updates completion:(void (^
__nullable)(BOOL finished))completion;
// allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.

// Support for reordering
- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0);
// returns NO if reordering was prevented from beginning - otherwise YES
- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition NS_AVAILABLE_IOS(9_0);
- (void)endInteractiveMovement NS_AVAILABLE_IOS(9_0);
- (void)cancelInteractiveMovement NS_AVAILABLE_IOS(9_0);

#ifndef SDK_HIDE_TIDE
// Support for Focus
@property (nonatomic)
BOOL remembersLastFocusedIndexPath NS_AVAILABLE_IOS(9_0);
// defaults to NO. If YES, when focusing on a collection view the last focused index path is focused automatically. If the collection view has never been focused, then the preferred focused index path is used.
#endif
@end

@interface NSIndexPath (UICollectionViewAdditions)

+ (instancetype)indexPathForItem:(NSInteger)item inSection:(NSInteger)section NS_AVAILABLE_IOS(6_0);

@property (nonatomic,
readonly) NSInteger item NS_AVAILABLE_IOS(6_0);

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: