文/Jacob_Pan(简书作者)
原文链接: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)的更多相关文章

  1. ios中的界面跳转方式

    ios中,两种界面跳转方式 1.NavgationController本身可以作为普通ViewController的容器,它有装Controller的栈,所以可以push和pop它们,实现你所说的跳转 ...

  2. 如何在ios中集成微信登录功能

    在ios中集成微信的登录功能有两种方法 1 用微信原生的api来做,这样做的好处就是轻量级,程序负重小,在Build Settings 中这样设置 然后设置 友盟的设置同上,但是要注意,加入你需要的所 ...

  3. iOS 中各种横竖屏切换总结

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...

  4. iOS中基于协议的路由设计

    一.背景 前段时间对我们自己的App做了结构上的重构,抛弃了之前简单的MVC开发模式,原因是随着App的业务线越来越多,单个页面的功能越来越复杂,MVC开发模式导致整个Controller-layer ...

  5. iOS中的主要框架framework

    在日常的iOS项目开发中,主要使用的就是Foundation和UIKit这两个框架. (一)Foundation框架 Foundation是对Core Foundation框架的一个封装,使用Foun ...

  6. IOS中调用系统的电话、短信、邮件、浏览功能

    iOS开发系列--通讯录.蓝牙.内购.GameCenter.iCloud.Passbook系统服务开发汇总 2015-01-13 09:16 by KenshinCui, 26990 阅读, 35 评 ...

  7. DEV控件中GridView中的复选框与CheckBox实现联动的全选功能

    最初的界面图如图1-1(全选框ID: cb_checkall  DEV控件名称:gcCon ): 要实现的功能如下图(1-2  1-3  1-4)及代码所示: 图1-2 图1-3 图1-4 O(∩_∩ ...

  8. iOS 8 中如何集成 Touch ID 功能

    2013年9月,苹果为当时发布的最新iPhone产品配备了一系列硬件升级方案.在iPhone 5s当中,最具创新特性的机制无疑要数围绕Home按钮设计的超薄金属圈,也就是被称为Touch ID的指纹传 ...

  9. freemarker中修改和添加功能中包含checkbox复选框默认选中需求的解决方式

    今天做的公司ERP系统上线第一天内部使用的,各种BUG铺天盖地,[虽然只是技术总监一个人在测试……],其中有一个就是其中部门管理页面中的修改和添加功能 一个人做一套ERP总是有点疏漏的,虽然里面的东西 ...

随机推荐

  1. Linux下一些基本操作

    一.忘记root密码 1. sudo passwd root 2. 输入新密码. 二.查看内核版本: 1.查看内核版本命令:1) cat /proc/version 2) uname -a 3)  u ...

  2. Intent 意图 结构 简介

    Intent简介 官方解释: An intent is an abstract description of an operation操作 to be performed展示.表演. It can b ...

  3. Beacon浅析

    作者:hongbosun 一.Beacon简介 Beacon是基于BLE技术实现的物理设备.BLE(全称Bluetooth Low Energy)是蓝牙4.0技术规范的一部分.它起源于Nokia的Wi ...

  4. ORA-25153: Temporary Tablespace is Empty解决方法

    SQL> @/tmp/4.txt create table huang_1 (deptno number,dname varchar2(19),loc varchar2(20)) * ERROR ...

  5. js带箭头左右翻动控制

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. MySQL 聚簇索引

    聚簇索引并不是一种单独的索引类型,而是一种数据存储方式.具体的细节依赖于其实现方式,但innoddb 的聚簇索引实际上在同一个结构中保存了B-Tree索引和数据行. 当表有聚簇索引时,它的数据实际上存 ...

  7. 服务器是windows时tomcat无法打印所有日志配置修改

    Tomcat运行仅一天磁盘空间突然就增加了很多,发现是日志文件太大了,修改tomcat的日志配置即可. 查看目录所占空间大小: ? 1 [root@XXX webapps]du -sh 清理方法: ? ...

  8. 阿里大鱼simplexmlelement object 取值PHP

    SimpleXMLElement Object(    [code] => 15    [msg] => Remote service error    [sub_code] => ...

  9. php 文件操作中几种方法整理

    1.获取文件夹下所有文件个数 echo ShuLiang("../0503lianxi"); function ShuLiang($filename) { if(is_dir($f ...

  10. Entity Framework With Mysql 之Code First

    Entity Framework 4.0现在也可以支持Mysql数据库了,这篇文章将向你展示如何用Code First的方式来实现. 1.首先新建一个项目,在项目中用NuGet添加如下引用: 2.在w ...