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#_Socket网络编程实现的简单局域网内即时聊天,发送文件,抖动窗口。
最近接触了C#Socket网络编程,试着做了试试(*^__^*) 实现多个客户端和服务端互相发送消息 发送文件抖动窗口功能 服务端: using System; using System.Collec ...
- inline-block容器的高度撑开位置
block的高度是从最上面撑开的 那么inline-block呢? 直接上代码 <!doctype html> <html> <head> <meta cha ...
- Codeforces Round #279 (Div. 2)f
树形最大上升子序列 这里面的上生子序列logn的地方能当模板使 good #include<iostream> #include<string.h> #include< ...
- Github提交代码及分支管理
#提交代码cd <Cat>git initgit add .git statusgit commit -m "注释"git remote add origin < ...
- 表单验证提交——submit与button
之前做东西接触过表单验证提交,但是都是为了完成工作,做完就做完了,没有注过表单验证提交有几种方法,各方法都有啥区别.今天瞎折腾了一下,对他们研究了一下,如下是我个人的理解: submit: 从字面上看 ...
- 转载:mysql-Auto_increment值修改
转载网址:http://libo93122.blog.163.com/blog/static/1221893820125282158745/ | 2012-03-13 11:19:10 | 2012- ...
- php DOMDocument 递归 格式化缩进HTML文档
function format(\DOMNode $node, $treeIndex = 0) { //不格式化的标签 if (in_array($node->nodeName, array(& ...
- transition Css3过度详解
过度语法: .example { transition-property: background-color; //需要过度的css属性 transition-duration: 2s; //过度所需 ...
- [转载]css3属性box-sizing:border-box的作用
http://jastate.com/css3-properties-box-sizingborder-box.html 定义和用法 按照w3c school的说法,box-sizing:border ...
- ./configure 时候报错c++ 编译器不能执行
./configure时报错:configure: error: C++ compiler cannot create executables .哎,今天重装测试服务器上的系统,设置好IP可以远程访问 ...