UICollectionView 基础篇

         属性:

         collectionViewLayout:集合视图布局

         backgroundView:背景视图

         allowsSelection:是否允许选中

         allowsMultipleSelection:是否允许多选

         方法:

         - (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout;初始化

         - (void)registerClass:(nullable Class)cellClass forCellWithReuseIdentifier:(NSString *)identifier;从一个类里面注册
         - (void)registerNib:(nullable UINib *)nib forCellWithReuseIdentifier:(NSString *)identifier;从一个nib里面注册

         - (void)registerClass:(nullable Class)viewClass forSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier;从一个类里面注册头视图或者尾视图
         - (void)registerNib:(nullable UINib *)nib forSupplementaryViewOfKind:(NSString *)kind withReuseIdentifier:(NSString *)identifier;从nib里面注册一个头视图或者尾视图
         - (__kindof UICollectionViewCell *)dequeueReusableCellWithReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;复用单元格
         - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind withReuseIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath;复用头视图或者尾视图

         - (nullable NSArray<NSIndexPath *> *)indexPathsForSelectedItems; 获取所有选中的单元格的索引
         - (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;设置选中某一item,并使视图滑动到相应位置,scrollPosition是滑动位置的相关参数
         - (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;将某一item取消选中
         - (void)reloadData; 更新数据
         - (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated; 重新设置布局方式
         - (void)setCollectionViewLayout:(UICollectionViewLayout *)layout animated:(BOOL)animated completion:(void (^ __nullable)(BOOL finished))completion (ios7);重新设置布局并设置回调
         - (UICollectionViewTransitionLayout *)startInteractiveTransitionToCollectionViewLayout:(UICollectionViewLayout *)layout completion:(nullable UICollectionViewLayoutInteractiveTransitionCompletion)completion(ios7);布局更改后的动画进行设置
         - (void)finishInteractiveTransition (ios7);准备好动画设置后,我们需要调用下面的方法进行布局动画的展示,之后会调用上面方法的block回调
         - (void)cancelInteractiveTransition (ios7);调用这个方法取消上面的布局动画设置,之后也会进行上面方法的block回调

         - (NSInteger)numberOfSections;  获取分区数
         - (NSInteger)numberOfItemsInSection:(NSInteger)section;获取某一分区的item数
         - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath;获取item的layout属性
         - (nullable UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;获取头尾视图的layout属性
         - (nullable NSIndexPath *)indexPathForItemAtPoint:(CGPoint)point;获取某一点所在的indexpath位置
         - (nullable NSIndexPath *)indexPathForCell:(UICollectionViewCell *)cell;获取某个cell所在的indexPath
         - (nullable UICollectionViewCell *)cellForItemAtIndexPath:(NSIndexPath *)indexPath;获取某个indexPath获取cell
         - (NSArray<__kindof UICollectionViewCell *> *)visibleCells;获取所有可见cell的数组
         - (NSArray<NSIndexPath *> *)indexPathsForVisibleItems;获取所有可见cell的索引数组

    下面三个方法用于获取尾视图
         - (UICollectionReusableView *)supplementaryViewForElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath (ios9);
         - (NSArray<UICollectionReusableView *> *)visibleSupplementaryViewsOfKind:(NSString *)elementKind(ios9);
         - (NSArray<NSIndexPath *> *)indexPathsForVisibleSupplementaryElementsOfKind:(NSString *)elementKind(ios9);

         - (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UICollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;使视图滑动到某一位置,可以带动画效果

         - (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;移动项
         - (void)performBatchUpdates:(void (^ __nullable)(void))updates completion:(void (^ __nullable)(BOOL finished))completion;编辑处理一个cell(曾,删,改)之后回调

         - (BOOL)beginInteractiveMovementForItemAtIndexPath:(NSIndexPath *)indexPath;开始在特定的索引上面对cell进行交互式工作 (ios9);
         - (void)updateInteractiveMovementTargetPosition:(CGPoint)targetPosition ;在手势作用期间更新交互移动的目标位置(ios9)
         - (void)endInteractiveMovement; 在完成手势动作后,结束交互式移动(ios9)
         - (void)cancelInteractiveMovement;取消交互式移动

         - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;定义cell的个数
         - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;定义cell的显示内容

         - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView;设定组的个数
         - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath;设置头视图或者尾视图
         - (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath (ios9);设置表中某些cell能否被移动
         - (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath;将表格中的某个cell移动到一个位置

         - (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath;是否显示cell的高亮状态
         - (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath;在cell高亮状态下执行的方法
         - (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath;在cell结束高亮状态下执行的方法
         - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;cell是否能背选中
         - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; cell是否能被取消选中
         - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath;cell被选中后执行的动作
         - (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath;cell被取消选中后执行的动作
         - (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath将要加载某个cell时执行的方法(ios8);
         - (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath(ios8);将要加载头尾视图的时候加载的方法
         - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;已经展示了某个cell执行的方法
         - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;已经展示了某个头视图执行的方法
         - (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;点击菜单按钮后的触发方法

         - (nonnull UICollectionViewTransitionLayout *)collectionView:(UICollectionView *)collectionView transitionLayoutForOldLayout:(UICollectionViewLayout *)fromLayout newLayout:(UICollectionViewLayout *)toLayout;重新进行布局时调用的方法
         - (BOOL)collectionView:(UICollectionView *)collectionView canFocusItemAtIndexPath:(NSIndexPath *)indexPath (ios9);是否可以同个某个焦点获取到路径
         - (BOOL)collectionView:(UICollectionView *)collectionView shouldUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context (ios9);是否可以更新焦点
         - (void)collectionView:(UICollectionView *)collectionView didUpdateFocusInContext:(UICollectionViewFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator (ios9);更新完焦点执行的方法
         - (nullable NSIndexPath *)indexPathForPreferredFocusedViewInCollectionView:(UICollectionView *)collectionView (ios9); 获取焦点的首选路径
         - (NSIndexPath *)collectionView:(UICollectionView *)collectionView targetIndexPathForMoveFromItemAtIndexPath:(NSIndexPath *)originalIndexPath toProposedIndexPath:(NSIndexPath *)proposedIndexPath (ios9);获取移动后的cell的索引
         - (CGPoint)collectionView:(UICollectionView *)collectionView targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset;自动对齐网格

         

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值