iOS中的界面多选功能--(UICollectionView)
原文链接:http://www.jianshu.com/p/9d28ebd0f5a2
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
最近做项目接触了一些需要实现多选的功能,但总结起来方法类似;实现此功能的方法有很多,接下来介绍一种方法,利用UICollectionView实现。

我们都知道,UICollectionView可以被认为更高级的UITableView,因此UITableView里面可以实现的在UICollectionView都可以实现,尤其针对类似瀑布流那样的界面,UICollectionView功能更强大,更方便。
本文没有介绍UICollectionView的Cell定制,代理的设置,数据模型,userView的封装等,如有兴趣可参照下面我做的简易Demo。

对于多选功能,显然我们会用到UICollectionView的两个方法:
- (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
同时用户可能会多次选择、取消操作,也就是说我们允许多次点击(multiple touch),为了更好处理这样操作,UICollectionView提供方法:
- (void)performBatchUpdates:(void (^ __nullable)(void))updates completion:(void (^ __nullable)(BOOL finished))completion; // allows multiple insert/delete/reload/move calls to be animated simultaneously. Nestable.
注意:在建立UICollectionView时,它的allowsMultipleSelection属性一定设置成YES。

在Demo中创建了一个Button,点击全选,按钮title改变,再次点击全部取消,此时需要对UICollectionView中的indexPath.item进行遍历,则创建了一个NSMutableIndexSet来增加和删除:
@property (nonatomic, strong) NSMutableIndexSet* selectedIndexSet;
当前没有选择时,我们会把它加入进去;选择后再次选择,会删除它。部分代码如下:
if ([self collectionView:self.contactsPickerView shouldSelectItemAtIndexPath:indexPath]) {
[self.contactsPickerView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self.selectedIndexSet addIndex:indexPath.item];
}
if ([self collectionView:self.contactsPickerView shouldDeselectItemAtIndexPath:indexPath]) {
[self.contactsPickerView deselectItemAtIndexPath:indexPath animated:YES];
[self.selectedIndexSet removeIndex:indexPath.item];
}
到此为止,读者也许已经想到,针对全选和全不选,只要遍历即可,下面为本人用的方法:
全选:for (NSUInteger index = 0; index < count; ++index) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldSelectItemAtIndexPath:indexPath]) {
[self.contactsPickerView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self.selectedIndexSet addIndex:indexPath.item];
}
}
全不选:[self.selectedIndexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL * _Nonnull stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldDeselectItemAtIndexPath:indexPath]) {
[self.contactsPickerView deselectItemAtIndexPath:indexPath animated:YES];
[self.selectedIndexSet removeIndex:indexPath.item];
}
}];

按钮标题也要随之改变,因此综上所述按钮的实现方法为:
- (IBAction)handleToggleSelectionBtn:(id)sender {
NSUInteger count = [self.contacts count];
BOOL allEnabledContactsSelected = [self allEnabledContactsSelected];
if (!allEnabledContactsSelected) {
[self.contactsPickerView performBatchUpdates:^{
for (NSUInteger index = 0; index < count; ++index) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldSelectItemAtIndexPath:indexPath]) {
[self.contactsPickerView selectItemAtIndexPath:indexPath animated:YES scrollPosition:UICollectionViewScrollPositionNone];
[self.selectedIndexSet addIndex:indexPath.item];
}}} completion:^(BOOL finished) {
[self updateToggleSelectionButton];
}];} else {
[self.contactsPickerView performBatchUpdates:^{
[self.selectedIndexSet enumerateIndexesUsingBlock:^(NSUInteger index, BOOL * _Nonnull stop) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
if ([self collectionView:self.contactsPickerView shouldDeselectItemAtIndexPath:indexPath]) {
[self.contactsPickerView deselectItemAtIndexPath:indexPath animated:YES];
[self.selectedIndexSet removeIndex:indexPath.item];
}}];} completion:^(BOOL finished) {
[self updateToggleSelectionButton];
}];}}
在此基本功能已经实现,但详细具体细节本文没有给出,只是给出一种思路;如果现在你的感觉是:

不要着急:请看Demo
iOS中的界面多选功能--(UICollectionView)的更多相关文章
- ios中的界面跳转方式
ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...
- 如何在ios中集成微信登录功能
在ios中集成微信的登录功能有两种方法 1 用微信原生的api来做,这样做的好处就是轻量级,程序负重小,在Build Settings 中这样设置 然后设置 友盟的设置同上,但是要注意,加入你需要的所 ...
- iOS 中各种横竖屏切换总结
iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...
- iOS中基于协议的路由设计
一.背景 前段时间对我们自己的App做了结构上的重构,抛弃了之前简单的MVC开发模式,原因是随着App的业务线越来越多,单个页面的功能越来越复杂,MVC开发模式导致整个Controller-layer ...
- iOS中的主要框架framework
在日常的iOS项目开发中,主要使用的就是Foundation和UIKit这两个框架. (一)Foundation框架 Foundation是对Core Foundation框架的一个封装,使用Foun ...
- IOS中调用系统的电话、短信、邮件、浏览功能
iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...
- DEV控件中GridView中的复选框与CheckBox实现联动的全选功能
最初的界面图如图1-1(全选框ID: cb_checkall DEV控件名称:gcCon ): 要实现的功能如下图(1-2 1-3 1-4)及代码所示: 图1-2 图1-3 图1-4 O(∩_∩ ...
- iOS 8 中如何集成 Touch ID 功能
2013年9月,苹果为当时发布的最新iPhone产品配备了一系列硬件升级方案.在iPhone 5s当中,最具创新特性的机制无疑要数围绕Home按钮设计的超薄金属圈,也就是被称为Touch ID的指纹传 ...
- freemarker中修改和添加功能中包含checkbox复选框默认选中需求的解决方式
今天做的公司ERP系统上线第一天内部使用的,各种BUG铺天盖地,[虽然只是技术总监一个人在测试……],其中有一个就是其中部门管理页面中的修改和添加功能 一个人做一套ERP总是有点疏漏的,虽然里面的东西 ...
随机推荐
- c# 操作PPT
前段时间要做一个把指定图片放到新建的ppt的东西,在网上找了点资料看了一下,发现用C#做好像是最简单的一个,一下是在网上找的一段代码,直接贴进去就能够执行,可是在执行之前一定要加入dll支持: 项目 ...
- somethings about QSplitter
m_splitter = new QSplitter(Qt::Horizontal); m_splitter->addWidget(this->m_leftWidget); m ...
- IE下判断IE版本语法使用
先摆一下判断IE版本语法 <!--[if lte IE 6]> <![endif]--> IE6及其以下版本可见 <!--[if lte IE 7]> <:由多个[事务]组成: 一个[事务](IN.OUT.SETUP):由一多个[Packet]组成. USB数据在[主机软件]与[USB设备特定的端点 ...
- C#。1 数据类型,常量变量,类型转换
C#. 一.数据类型 1,字符串类型(string) .放入一串字符串,需要用""引起来. 列如: string a ="999"; 2,整型 (int). ...
- JS笔记-常见函数与问题
1.请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组. Array.prototype.distinct = functi ...
- seajs原理解析
一: 1.本文是基于seajs2.2.1编写的,之后版本应该大同小异 2.本文仅代表个人观点,如有理解错误,敬请指出,大家一起学习 二: 1.首先放一张我画的流程图 这是我理解的seajs的基本的所有 ...
- c++中vector等容器的实现机制
stl容器区别: vector list deque set map-底层实现 stl容器区别: vector list deque set map (转) 在STL中基本容器有: vector.li ...
- PHP页面中文乱码问题
首先纯html页要用meta标签声明编码<meta http-equiv="Content-Type" content="text/html; charset=&q ...