iOS中的界面多选功能--(UICollectionView)
原文链接:http://www.jianshu.com/p/9d28ebd0f5a2
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
最近做项目接触了一些需要实现多选的功能,但总结起来方法类似;实现此功能的方法有很多,接下来介绍一种方法,利用UICollectionView实现。
![](http://upload-images.jianshu.io/upload_images/310248-fae8c79938b54301.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
我们都知道,UICollectionView可以被认为更高级的UITableView,因此UITableView里面可以实现的在UICollectionView都可以实现,尤其针对类似瀑布流那样的界面,UICollectionView功能更强大,更方便。
本文没有介绍UICollectionView的Cell定制,代理的设置,数据模型,userView的封装等,如有兴趣可参照下面我做的简易Demo。
![](http://upload-images.jianshu.io/upload_images/310248-c304809cb3024605.gif?imageMogr2/auto-orient/strip)
对于多选功能,显然我们会用到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。
![](http://upload-images.jianshu.io/upload_images/310248-3a645fcb4fd8a46e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在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];
}
}];
![](http://upload-images.jianshu.io/upload_images/310248-e3df25b3f213fc7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
按钮标题也要随之改变,因此综上所述按钮的实现方法为:
- (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];
}];}}
在此基本功能已经实现,但详细具体细节本文没有给出,只是给出一种思路;如果现在你的感觉是:
![](http://upload-images.jianshu.io/upload_images/310248-0bc019f20244d22f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
不要着急:请看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总是有点疏漏的,虽然里面的东西 ...
随机推荐
- PLSQL Developer安装(Oracle11g+win7_64bit)
1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0)下载地址:http://www.oracle.co ...
- iptables简述
一.linux防火墙基础防火墙分为硬件防火墙和软件防火墙. 1.概述linux 防火墙体系主要工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙. 包过滤机制:ne ...
- Intent 意图 结构 简介
Intent简介 官方解释: An intent is an abstract description of an operation操作 to be performed展示.表演. It can b ...
- 使用SQL语句创建和删除约束
原文:http://blog.csdn.net/hamber_bao/article/details/6504905 约束的目的就是确保表中的数据的完整性. 常用的约束类型如下: 主键约束:(Prim ...
- Kinect研究
1. 深度照相机 红外 + 2个摄像头. (1彩色摄像头,1红外发射,1接收) 红外捕捉深度. 2. OpenNI 某编程社区创建. 提供基本深度数据的访问.LGPL协议. 用户跟踪付费. 3. 问题 ...
- java下properties属性文件操作
package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...
- linux mint 下mysql中文支持问题
一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /e ...
- jquery1.9学习笔记 之层级选择器(二)
子孙选择器(“祖先 子孙”) 描述:选择所有给出祖先选择器的子孙选择器. 例子: 用蓝色虚线边框标记所有表单子孙元素的输入.表单里的输入框用黄色背景. <!doctype html>< ...
- windows下配置lamp环境(4)---安装MySQL数据库5.6
图解安装MySQL数据库 1.获取软件就多不说了 2.双击开始安装 3.点击点击NEXT进行下一步 4.同意协议,点击NEXT进入下一步 5.选择指定安装位置安装方法,进入安装位值选择页面: 6.分别 ...
- (转载)MVC 4.0 PartialView()与View()真的一样吗?
转载自:http://www.cnblogs.com/lori/ 当我们使用razor作为页面引擎时,它的视图文件扩展名为cshtml或者vbshtml,而之前作为分部视图的ascx文件,进行razo ...