iOS UITableViewCell的"滑动出现多个按钮"
本文授权转载,作者:@夏天是个大人了
前言: 本篇博客其实就是想介绍tableviewcell滑动的一些"事",昨天在逛github的时候看到的还挺有意思的三方库,简单用了一下感觉不错,一作为记录,二是希望有类似需求的可以得到帮助。
本篇介绍了iOS 5之后(使用三方库) iOS 8之后(系统方法)分别的实现方式

效果图 - ios>= 5.0

效果图 - ios>= 8.0
MGSwipeTableCell(Github上的三方库)- iOS >= 5.0
直接使用比较简单 通过代码看一下
首先签这个协议MGSwipeTableCellDelegate
添加左边按钮方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
- (NSArray *)btnLeftCount:(int)count{ NSMutableArray *result = [NSMutableArray array]; UIColor *colors[3] = {[UIColor greenColor], [UIColor colorWithRed:0 green:0x99/255.0 blue:0xcc/255.0 alpha:1.0], [UIColor colorWithRed:0.59 green:0.29 blue:0.08 alpha:1.0]};; for (int i = 0; i < count; i ++) { // 按钮提供了几个方法, 可以点进去看一看 MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:@"" backgroundColor:colors[i] padding:15 callback:^BOOL(MGSwipeTableCell *sender) { return YES; }]; // 把按钮加到数组中 [result addObject:btn]; } return result;} |
添加右边按钮的方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
- (NSArray *)btnRightCount:(int)count{ NSMutableArray *result = [NSMutableArray array]; NSArray *titleArray = @[@"删除", @"标记未读"]; UIColor *color[2] = {[UIColor redColor], [UIColor lightGrayColor]}; for (int i = 0; i < count; i ++) { MGSwipeButton *btn = [MGSwipeButton buttonWithTitle:titleArray[i] backgroundColor:color[i] padding:15 callback:^BOOL(MGSwipeTableCell *sender) { BOOL autoHide = i != 0; return autoHide; }]; // 把按钮加到数组中 [result addObject:btn]; } return result;} |
重用池可以这样写
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellID = @"cellId"; // 这里如果MGSwipeTableCell是足够你使用的, 你可以直接使用 // 或者自定义创建cell继承于MGSwipeTableCell, 像我下面代码这样 XTCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; if (!cell) { cell = [[XTCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; } cell.label.text = [NSString stringWithFormat:@"%ld------%@", (long)indexPath.row, self.arrayTest[indexPath.row]]; cell.label.font = [UIFont systemFontOfSize:20]; // 指定代理人 cell.delegate = self; // NO: 只有单个可以滑动 , YES: 多个可以滑动 cell.allowsMultipleSwipe = NO; return cell;} |
添加按钮
|
1
2
3
4
5
6
7
8
9
10
|
-(NSArray*) swipeTableCell:(MGSwipeTableCell*) cell swipeButtonsForDirection:(MGSwipeDirection)direction swipeSettings:(MGSwipeSettings*) swipeSettings expansionSettings:(MGSwipeExpansionSettings*) expansionSettings;{ if (direction == MGSwipeDirectionRightToLeft) { return [self btnRightCount:2]; } else { return [self btnLeftCount:3]; }} |
按钮的点击代理方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
-(BOOL) swipeTableCell:(MGSwipeTableCell*) cell tappedButtonAtIndex:(NSInteger) index direction:(MGSwipeDirection)direction fromExpansion:(BOOL) fromExpansion{ switch (direction) { case MGSwipeDirectionLeftToRight: { if (index == 0) { NSLog(@"right ------- 0"); }else{ NSLog(@"right ------- 1"); } break; } case MGSwipeDirectionRightToLeft: { if (index == 0) { NSLog(@"left ------- 0"); // 这里简单的做了个删除操作 NSIndexPath * path = [_tableView indexPathForCell:cell]; [_arrayTest removeObjectAtIndex:path.row]; [_tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft]; return NO; }else{ NSLog(@"left ------- 1"); } break; } } return YES;} |
iOS 8之后也提供了类似的实现
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
- (NSArray<uitableviewrowaction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.arrayTest removeObjectAtIndex:indexPath.row]; [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; }]; UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.arrayTest exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0]; NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section]; [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath]; }]; topRowAction.backgroundColor = [UIColor blueColor]; UITableViewRowAction *moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"更多" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) { [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle]; }]; return @[deleteRowAction,topRowAction,moreRowAction]; |
iOS UITableViewCell的"滑动出现多个按钮"的更多相关文章
- iOS系列 基础篇 06 标签和按钮 (Label & Button)
iOS系列 基础篇 06 标签和按钮 (Label & Button) 目录: 标签控件 按钮控件 小结 标签和按钮是两个常用的控件,下面咱们逐一学习. 1. 标签控件 使用Single Vi ...
- 纯css隐藏移动端滚动条解决方案(ios上流畅滑动)
纯css隐藏移动端滚动条解决方案(ios上流畅滑动) html代码展示(直接复制代码保存至本地文件运行即可): <!DOCTYPE html> <html lang="en ...
- QSplitter实现滑动窗口和悬浮按钮
1 QSplitter实现滑动窗口和悬浮按钮 软件应用中需要设计右侧滑动窗口,通过一个按钮来实现窗口的隐藏和显示,应用场景比如显示主界面的详细信息. (1) 在qt design中 ...
- iOS点击获取短信验证码按钮
概述 iOS点击获取短信验证码按钮, 由于 Demo整体测试运行效果 , 整个修改密码界面都已展现, 并附送正则表达式及修改密码逻辑. 详细 代码下载:http://www.demodashi.com ...
- iOS UITableViewCell UITableVIewController 纯代码开发
iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...
- 纯css隐藏移动端滚动条解决方案(ios上流畅滑动)---转载
html代码展示(直接复制代码保存至本地文件运行即可): <!DOCTYPE html> <html lang="en"> <head> < ...
- iOS UITableViewCell滑动删除
一般我们使用列表的形式展现数据就会用到UITableView.在熟练掌握了用UITableView展示数据以后,开发过程中可能会遇到需要删除数据的需求,我们想实现在一行数据上划动一下,然后出现一个删除 ...
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang
转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...
- iOS手势(滑动)返回的实现(自定义返回按钮)
如果各位使用的是storyboard布局的话,且用的是系统的返回按钮,那么是自动会有滑动返回效果的,但是相信各位做项目的,一般都是用的自定义的返回按钮,所以我贴几行代码,看看怎么实现系统自带的滑动返回 ...
随机推荐
- java 深入技术二(Collection)
1. java集合 存储和管理多个java对象 包括很多java类和接口 Collection List Set ArrayList Lin ...
- 2014 39th ACM-ICPC 西安赛区 总结
西安,打铁. 出发前听说是大赛区,签到的时候看了秩序册的队伍情况,264支队伍. 在听说是大赛区之前,我觉得我们队应该是银首,运气好+发挥超常的话或许有金,即保银冲金. 听到大赛区之后,觉得可能金区有 ...
- 总是有一个程序的bug没找到
算法训练 Lift and Throw 时间限制:3.0s 内存限制:256.0MB 问题描述 给定一条标有整点(1, 2, 3, ...)的射线. 定义两个点之间的距离为其下标之 ...
- JDK7 tomcat6 各种环境变量配置
JDK JAVA_HOME:jdk路径,bin目录上一层 Path:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin CLASSPATH:.;%JAVA_HOME%\lib;%J ...
- VR技术的系统化实现阶段
转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 从20世纪80年代至80年代中期,虚拟现实技术的基本概念开始逐渐形成和完善.这一时期出现了一些比较经典的 ...
- IIS使用Tip
1. IIS7 HTTPS 绑定主机头 IIS7下面默认HTTPS绑定是无法指定主机头的,但我们可以通过手工修改IIS配置来实现主机头绑定. 打开C:\Windows\system32\inetsrv ...
- [BI项目记]-对项目文件进行规划
BI项目中会有很多不同种类的项目,此篇会对这些项目进行一个总结,并且在TFS中进行分类. 根据笔者对BI项目的经验和理解,主要将BI项目中的项目类型进行如下分类: DB脚本 DB脚本是BI项目中重要的 ...
- linux 内核与用户空间通信之netlink使用方法
转自:http://blog.csdn.net/haomcu/article/details/7371835 Linux中的进程间通信机制源自于Unix平台上的进程通信机制.Unix的两大分支AT&a ...
- mysql数据库的安装与使用
ubuntu下面的mysql安装 sudo apt-get install mysql-server 安装后,登陆 mysql -u root -p mysql -h 主机名 -u 用户名 -p -h ...
- 【CentOS】正则表达式
1.grep [-cinvABC] 'word' filename -c :打印符合要求的行数 --color:显示颜色 -i :忽略大小写(ignore) -n :在输出符合要求的行的同时连同 ...