写在前面

collectionView的多选是个老生常谈的问题了,但是当首次实现的时候还是要注意点什么的。

实现多选

这俩属性控制这些items是否可以选择,如果是,是否可以同时选择多个。

// These properties control whether items can be selected, and if so, whether multiple items can be simultaneously selected.
@property (nonatomic) BOOL allowsSelection; // default is YES
@property (nonatomic) BOOL allowsMultipleSelection; // default is NO

只需要 allowsMultipleSelection = YES你的collectionView就支持多选了。
你可能会问,我怎么没看到,看这里
collectionView有一个属性,保存着被选中item的indexPath

@property (nonatomic, readonly, nullable) NSArray<NSIndexPath *> *indexPathsForSelectedItems; // returns nil or an array of selected index paths

在这两个方法里面打印一下,就会看到,点击选中,点击取消选中,记录的一点都没错。-collectionView: didSelectItemAtIndexPath:
collectionView: didDeselectItemAtIndexPath:
那么怎么让他显示出来呢,接着去看UICollectionViewCell的API,通过重写这两个方法,你不仅可以自定义selected的UI,还可以自定义highlighted的

// Cells become highlighted when the user touches them.
// The selected state is toggled when the user lifts up from a highlighted cell.
// Override these methods to provide custom UI for a selected or highlighted state.
// The collection view may call the setters inside an animation block.
@property (nonatomic, getter=isSelected) BOOL selected;
@property (nonatomic, getter=isHighlighted) BOOL highlighted;

要记得先调用父类的

- (void)setSelected:(BOOL)selected {[super setSelected:selected];self.backgroundColor = selected ? [UIColor yellowColor] : [UIColor cyanColor];
}

到此结束,你只需要做这么多。

全选,取消全选

这里用到了四个方法

  • - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
  • - (BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath; // called when the user taps on an already-selected item in multi-select mode

  • - (void)selectItemAtIndexPath:(nullable NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UICollectionViewScrollPosition)scrollPosition;

  • - (void)deselectItemAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated;

前两个根据自己的需要返回YES或NO,后面两个文档上有句话需要注意:
This method does not cause any selection-related delegate methods to be called.
意思说着两个方法不会导致任何选中,取消选中的代理方法被调用。刷新UI需要手动处理。

- (void)selectedAllorCancel:(UIBarButtonItem *)item {if ([item.title isEqualToString:@"全选"]) {[item setValue:@"取消" forKey:@"title"];[self shouldSelectedAll:YES];}else {[item setValue:@"全选" forKey:@"title"];[self shouldSelectedAll:NO];}
}- (void)shouldSelectedAll:(BOOL )selectedAll {for (NSIndexPath *indexPath in self.collectionView.indexPathsForVisibleItems) {if (selectedAll) {if ([self collectionView:self.collectionView shouldSelectItemAtIndexPath:indexPath]) {[self.collectionView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];LCRegionCell *cell = (LCRegionCell*)[self.collectionView cellForItemAtIndexPath:indexPath];cell.selected = YES;}}else {if ([self collectionView:self.collectionView shouldDeselectItemAtIndexPath:indexPath]) {[self.collectionView deselectItemAtIndexPath:indexPath animated:YES];LCRegionCell *cell = (LCRegionCell*)[self.collectionView cellForItemAtIndexPath:indexPath];cell.selected = NO;}}}
}

之前看说要用到这个方法,实际操作不用也没什么区别,感觉这里并不需要,还望告知解惑。

- (void)performBatchUpdates:(void (NS_NOESCAPE ^ _Nullable)(void))updates completion:(void (^ _Nullable)(BOOL finished))completion; // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.

注意:为节省时间这里整个collectionView的所有item遍历的是collectionView.indexPathsForVisibleItems,也就是说仅在当前屏幕上显示的item,项目根据需求去遍历

总结

总的来说collectionView很强大,可以这么说,凡是用tableView实现的用collectionView同样可以,支持的功能要比tableView要多。还有很多待发现的功能,api文档是最好的学习途径。

CollectionView多选相关推荐

  1. iOS 仿链家筛选(单选、多选、滑动筛选联动、多表联动)

    目前市场上很多应用都包含了筛选功能,自己写了个demo给大家分享一下,共同学习,共同进步 Demo传送门 先说说常见的collectionView多选功 - (void)collectionView: ...

  2. 最全的ORACLE-SQL笔记(转,出处不详)

    -- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...

  3. iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置

    转发自 http://www.cnblogs.com/pruple/p/5357150.html #import "ViewController.h" #import <Im ...

  4. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动

    两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...

  5. 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-10系统,3行代码即可集成....

    重要提示: 1. 1.9.0版本已发布,移除了"prefs:root="的调用,这个API已经被列为私有API,请大家尽快升级.其它同样使用了该API的库大家可以检查下,比如著名的 ...

  6. UICollectionView实现的图片的多选效果(本人已封装好,简单操作)

    github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准) 先说操作,有时间再说我的设计原理. 两种模式: 模 ...

  7. 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器

    代码地址如下: http://www.demodashi.com/demo/11689.html 重要提示: 1. 1.9.0版本已发布,移除了"prefs:root="的调用,这 ...

  8. IOS多选单选相册图片

    IOS多选单选相册图片 之前做项目让实现多选相册的图片,自己写了一个demo一直保存在电脑上,今天下午发现电脑128G的容量已经快没有了,准备清理电脑,所以把之前做的一些demo放在博客上,以后方便用 ...

  9. iOS开发------简单实现图片多选功能(Photos.framework篇)

    Photos.framework是iOS8后苹果推出的一套替代AssetsLibrary.framework获取相册资源的原生库,至于AL库,欢迎大家给博文iOS开发--简单实现图片多选功能(Asse ...

最新文章

  1. java内存分配和回收策略
  2. ITK:将样条曲线拟合到点集
  3. Apache JMeter 字体、字体大小修改
  4. 计算机控制系统课程设计中期报告,课程设计中期报告模板.doc
  5. 红旗linux 装xp,XP下硬盘安装红旗LINUX
  6. 项目实训第二周(车道线检测)
  7. itextpdf添加表格元素_基操勿6第四期:PPT表格美化
  8. 35. systemtap
  9. 软件各项会议评审意见模版
  10. 面试宝典(二)之经典面试题(含详细答案)
  11. 信息系统项目管理师考试重点汇总,看完这篇再拿十分!
  12. 【视频异常检测-论文阅读】Learning Not to Reconstruct Anomalies
  13. Vue与React的异同
  14. 一些互联网标准化组织
  15. 7-45 航空公司 VIP 客户查询(25 分)
  16. ifconfig安装
  17. 微信公众号只能设置两个网页授权域名的解决方案
  18. 田纳西大学计算机科学,田纳西大学_田纳西大学(University of Tennessee)
  19. 百度竞价排名曝光_百度爱采购的三大优势,及如何发优质商品。
  20. 常用的webservice公共接口

热门文章

  1. 中国改革开放30年创新人物--戴晔
  2. i7 12700k和i7 12700的区别 i712700k和12700性能差距
  3. WebSocket 数据帧
  4. 提高前端开发质量和效率的脚手架和工具套件 - Uix Kit
  5. 大学计算机基础码,大学计算机基础字符的编码——区位码和国标码
  6. 百花齐放的家居行业联盟,三翼鸟率先撬动三个赛点
  7. 下载NetSarang的Xshell (完全免费)
  8. 初窥Linux神秘面纱(叁):基本指令讲解(下)及疑难解惑(精)
  9. 交换机设备理论上线速转发的包转发率
  10. 【Python】哄女朋友的那些奇技淫巧