1.继承链:UIScrollView:UIview:UIResponder:NSObject
2.collection view使用的数据源需要遵守UICollectionViewDataSource协议,使用 UICollectionViewCell的实例对象来创建每个单独collection cell单元来显示数据源的每个单元视图
3.为了提高重用性,最好使用一下两种方法来创建视图:
(1)使用 dequeueReusableCellWithReuseIdentifier:forIndexPath:来得到collection cell
(2)使用 dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:来创建补充视图
注意:但是当你使用(1)的方法的时候首先需要配置好registerClass:forCellWithReuseIdentifier:
or registerNib:forCellWithReuseIdentifier:
method.这些方法,不然会报错
4.为了能够互相交互的对项目进行重新布置,可以使用 beginInteractiveMovementForItemAtIndexPath:方法;当你使用手势来跟踪触发事件的时候可以使用
updateInteractiveMovementTargetPosition:方法来返回触发的点;当你结束跟踪的时候可以使用
endInteractiveMovement
or cancelInteractiveMovement方法来结束交互和更新视图。
5. UICollectionViewController这个类提供有默认的手势管理器来管理重新排列collection view,为了能够使用手势你需要设置
installsStandardGestureForInteractiveMovement为yes。
6.初始化collection view
(1)- (instancetype)initWithFrame:(CGRect)frame
collectionViewLayout:(UICollectionViewLayout *)layout
:通过一个布局策略初识化CollectionView
7.配置collection view
(1)@property(nonatomic, weak) id< UICollectionViewDelegate> delegate :设置委托
(2)@property(nonatomic, weak) id< UICollectionViewDataSource> dataSource :设置数据源
(3)@property(nonatomic, strong) UIView*backgroundView :设置背景视图
8.创建collection cell
(1)- (void)registerClass:(Class)cellClass
forCellWithReuseIdentifier:(NSString *)identifier
:从一个class文件进行cell(item)的注册
(2)- (void)registerNib:(UINib *)nib
forCellWithReuseIdentifier:(NSString *)identifier
: 从一个nib文件进行cell(item)的注册
(3)//下面两个方法与上面相似,这里注册的是头视图或者尾视图的类
//其中第二个参数是设置 头视图或者尾视图 系统为我们定义好了这两个字符串
//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;
(4)- (__kindofUICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath
identifier |
The reuse identifier for the specified cell. This parameter must not be |
indexPath |
The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cell’s position in the collection view. |
解释:从复用池中取出cell
(5)- (__kindofUICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
withReuseIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath
elementKind |
The kind of supplementary view to retrieve. This value is defined by the layout object. This parameter must not be |
identifier |
The reuse identifier for the specified view. This parameter must not be |
indexPath |
The index path specifying the location of the supplementary view in the collection view. The data source receives this information when it is asked for the view and should just pass it along. This method uses the information to perform additional configuration based on the view’s position in the collection view. |
解释:从复用池中取出头尾视图
9.改变布局
(1)@property(nonatomic, strong) UICollectionViewLayout*collectionViewLayout :获取或设置布局
(2)- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout
animated:(BOOL)animated
:使用动画来重新设置布局
(3)- (void)setCollectionViewLayout:(UICollectionViewLayout *)layout
animated:(BOOL)animated
completion:(void (^)(BOOL finished))completion
:使用动画的方式来进行重新布局,并且结束后会返回一个布尔值确定转换是否成功
(4)- (UICollectionViewTransitionLayout *)startInteractiveTransitionToCollectionViewLayout:(UICollectionViewLayout *)layout
completion:(UICollectionViewLayoutInteractiveTransitionCompletion)completion :开始交互式转换来进行布局,这个方法传入一个布局策略layout,系统会开始进行布局渲染,返回一个UICollectionViewTransitionLayout对象
//这个UICollectionViewTransitionLayout对象管理动画的相关属性
(5)- (void)finishInteractiveTransition :准备好动画设置后,我们需要调用下面的方法进行布局动画的展示,之后会调用上面方法的block回调
(6)- (void)cancelInteractiveTransition ://调用这个方法取消上面的布局动画设置,之后也会进行上面方法的block回调
10.重新加载内容
(1)- (void)reloadData :重新加载数据
(2)- (void)reloadSections:(NSIndexSet *)sections
:重新加载分区
(3)- (void)reloadItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
:重新加载指定路径的item
11.获取collection view的状态
(1)- (NSInteger)numberOfSections :获取和设置分区的数量
(2)- (NSInteger)numberOfItemsInSection:(NSInteger)section
:获取或者设置分区俩面里面item的数量
(3)- (NSArray<__kindofUICollectionViewCell *> *)visibleCells :获取可见cell组成的数组
12.插入、移动、删除item
(1)- (void)insertItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
:插入指定路径的item组成的数组
(2)- (void)moveItemAtIndexPath:(NSIndexPath *)indexPath
toIndexPath:(NSIndexPath *)newIndexPath
:把indexpath路径的item移动到newindexpath
(3)- (void)deleteItemsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths
:删除指定路径的item组成的数组
13.插入、移动、删除分区
(1)- (void)insertSections:(NSIndexSet *)sections
:插入指定的分区
(2)- (void)moveSection:(NSInteger)section
toSection:(NSInteger)newSection
:把section分区到newsection分区
(3)- (void)deleteSections:(NSIndexSet *)sections
:删除指定的分区
14.重新排序item交互
(1)- (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath
:开始指定路径item的移动交互性
(2)- (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition
:更新目标地址的移动交互性
(3)- (void)endInteractiveMovement :结束移动交互性,移动item
(4)- (void)cancelInteractiveMovement :取消移动交互性
15.管理选择
(1)@property(nonatomic) BOOL allowsSelection :是否选择
(2)@property(nonatomic) BOOL allowsMultipleSelection :是否允许多重选择
(3)- (NSArray<NSIndexPath *> *)indexPathsForSelectedItems :包含由被选item的路径组成的数组
(4)- (void)selectItemAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
scrollPosition:(UICollectionViewScrollPosition)scrollPosition
:移动到被选路径的item
(5)- (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath
animated:(BOOL)animated
:取消指定路径的选择
16.管理focus
(1)@property(nonatomic) BOOL remembersLastFocusedIndexPath :记录最后被focus的item的路径
17.collection view和item的位置
(1)- (NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point
:返回进行点item的路径
(2)- (NSArray<NSIndexPath *> *)indexPathsForVisibleItems :返回由可见item组成的数组
(3)- (NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell
:获得指定cell的路径
(4)- (UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath
:获取指定路径的cell
(5)- (NSArray<NSIndexPath *> *)indexPathsForVisibleSupplementaryElementsOfKind:(NSString *)elementKind
:获取可见头尾视图的路径
(6)- (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind
atIndexPath:(NSIndexPath *)indexPath
:返回指定路径的头尾视图
(7)- (NSArray<UICollectionReusableView *> *)visibleSupplementaryViewsOfKind:(NSString *)elementKind
:返回可见的头尾视图
18.获取布局信息
(1)- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
:获取指定路径item的布局属性
(2)- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind
atIndexPath:(NSIndexPath*)indexPath
:获取指定路径头尾视图的布局属性
19.滚动到视图
(1)- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath
atScrollPosition:(UICollectionViewScrollPosition)scrollPosition
animated:(BOOL)animated
:使用动画的形式滚动到指定的路径,并设置滚动点的位置
20.collection view动画的多重变化
(1)- (void)performBatchUpdates:(void (^)(void))updates
completion:(void (^)(BOOL finished))completion
:同时用户可能会多次选择、取消操作,也就是说我们允许多次点击(multiple touch),为了更好处理这样操作,UICollectionView提供方法: