文/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. gcc和g++编译c或者c++文件碰到的问题

    gcc和g++都是GNU(组织)的一个编译器.        误区一:gcc只能编译c代码,g++只能编译c++代码      两者都可以,但是请注意:      1.后缀为.c的,gcc把它当作是C ...

  2. springmvc 基础

    在最简单的springmvc应用程序中,控制器是唯一需要在java web部署描述文件(web.xml)中配置的servlete(springmvc的控制器是Dispatcher Servlet).每 ...

  3. Multi-touch.

    TP(Multi-touch). 1.Win7有6个版本,分别如下. Windows 7 Starter (初级版)                                        不支 ...

  4. Linux的VI/VIM

    参考自:http://www.cnblogs.com/itech/archive/2009/04/17/1438439.html 作者:iTech 出处:http://itech.cnblogs.co ...

  5. OD: Exploit Me - Overwrite Nearby Varible

    实验代码: #include<stdio.h> #include<string.h> #define PASSWORD "1234567" int veri ...

  6. js判断值是否为数字

    js判断是否是数字 第一种方法 isNaN isNaN 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). NaN 即 Not a Number isNaN(numValu ...

  7. cocos2dx Sprite的多种创建方法

    1.通过文件创建 Sprite *bg = Sprite::create("backGround.jpg"); 2.通过图片的某个区域创建 SpriteFrame *frame = ...

  8. (四)JS学习笔记 - 模式 - 观察者模式

    理解观察者模式 简单的讲,一个对象作为特定任务或是另一对象的活动的观察者,并且在这个任务或活动发生时,利用事件的形式通知观察者. 观察者的使用场合 当一个对象的改变需要同时改变其它对象,并且它不知道具 ...

  9. 使用HTML5中的Canves标签制作时钟特效

    <!DOCTYPE html > <html> <head> </head> <body> <canvas id="cloc ...

  10. jquery多级联动(ajax查数据库)

    /id 代表下级下拉框ID,cityCode代表的是父级菜单代码,所有级菜单在同一张表,后台在加载是把菜单已经加入到Map缓存中.... //id 代表下级下拉框ID,cityCode代表的是父级菜单 ...