CollectionView多选
写在前面
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多选相关推荐
- iOS 仿链家筛选(单选、多选、滑动筛选联动、多表联动)
目前市场上很多应用都包含了筛选功能,自己写了个demo给大家分享一下,共同学习,共同进步 Demo传送门 先说说常见的collectionView多选功 - (void)collectionView: ...
- 最全的ORACLE-SQL笔记(转,出处不详)
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...
- iOS SDWEBImage和collectionView的组合,以及collectionView的随意间距设置
转发自 http://www.cnblogs.com/pruple/p/5357150.html #import "ViewController.h" #import <Im ...
- ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动
两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器,适配了iOS6-10系统,3行代码即可集成....
重要提示: 1. 1.9.0版本已发布,移除了"prefs:root="的调用,这个API已经被列为私有API,请大家尽快升级.其它同样使用了该API的库大家可以检查下,比如著名的 ...
- UICollectionView实现的图片的多选效果(本人已封装好,简单操作)
github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准) 先说操作,有时间再说我的设计原理. 两种模式: 模 ...
- 仿照微信的效果,实现了一个支持多选、选原图和视频的图片选择器
代码地址如下: http://www.demodashi.com/demo/11689.html 重要提示: 1. 1.9.0版本已发布,移除了"prefs:root="的调用,这 ...
- IOS多选单选相册图片
IOS多选单选相册图片 之前做项目让实现多选相册的图片,自己写了一个demo一直保存在电脑上,今天下午发现电脑128G的容量已经快没有了,准备清理电脑,所以把之前做的一些demo放在博客上,以后方便用 ...
- iOS开发------简单实现图片多选功能(Photos.framework篇)
Photos.framework是iOS8后苹果推出的一套替代AssetsLibrary.framework获取相册资源的原生库,至于AL库,欢迎大家给博文iOS开发--简单实现图片多选功能(Asse ...
最新文章
- java内存分配和回收策略
- ITK:将样条曲线拟合到点集
- Apache JMeter 字体、字体大小修改
- 计算机控制系统课程设计中期报告,课程设计中期报告模板.doc
- 红旗linux 装xp,XP下硬盘安装红旗LINUX
- 项目实训第二周(车道线检测)
- itextpdf添加表格元素_基操勿6第四期:PPT表格美化
- 35. systemtap
- 软件各项会议评审意见模版
- 面试宝典(二)之经典面试题(含详细答案)
- 信息系统项目管理师考试重点汇总,看完这篇再拿十分!
- 【视频异常检测-论文阅读】Learning Not to Reconstruct Anomalies
- Vue与React的异同
- 一些互联网标准化组织
- 7-45 航空公司 VIP 客户查询(25 分)
- ifconfig安装
- 微信公众号只能设置两个网页授权域名的解决方案
- 田纳西大学计算机科学,田纳西大学_田纳西大学(University of Tennessee)
- 百度竞价排名曝光_百度爱采购的三大优势,及如何发优质商品。
- 常用的webservice公共接口