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布局的话,且用的是系统的返回按钮,那么是自动会有滑动返回效果的,但是相信各位做项目的,一般都是用的自定义的返回按钮,所以我贴几行代码,看看怎么实现系统自带的滑动返回 ...
随机推荐
- RobotFrameWork(四)变量运算与Evaluate
一.特殊变量运算: 执行结果: 二.Evaluate使用 函数释义:Evaluate是执行Python表达式,并返回执行结果 示例1: 执行结果: 示例2: 执行结果:
- Deep Learning入门视频(上)_一层/两层神经网络code
关于在51CTO上的深度学习入门课程视频(9)中的code进行解释与总结: (1)单层神经网络: #coding:cp936 #建立单层神经网络,训练四个样本, import numpy as np ...
- vux 中popup 组件 Mask 遮罩在最上层问题的解决
1. 问题描述:popup弹出层在遮罩层下面的 2.原因:因为滚动元素和mask遮罩层在同一级,vux框架默认把遮罩层放在body标签下的 3.解决方法:更改一下源码,把mask遮罩层放在popup同 ...
- Android开发之---Activity生命周期
Android开发中,有四大组件:Activity.Service.Content Provider.Broadcast Receiver,可以说,activity的使用是最频繁的了,这里来梳理一下与 ...
- 在db2数据库上模拟死锁场景 还是z上的
如果条件允许,起两个线程互相抢资源就行了,但问题是,时间上还需要同步,要做到完美控制,还得加其他逻辑,忒费事,所以可以用下面的办法: 在目标表上直接加个锁……简单,粗暴,直接……很好…… LOCK T ...
- hdu 2594 Simpsons’ Hidden Talents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 思路:将两个串连起来求一遍Next数组就行长度为两者之和,遍历时注意长度应该小于两个串中的最小值 ...
- ajax实例2
前台: function save() { var username = document.getElementById("username").value; var id = d ...
- c#关于类的继承
public class D { public virtual void Run(string name) { Console.WriteLine(name + ",good"); ...
- 使用DotNetBar制作漂亮的WinFrom界面,自定义AgileEAS.NET SOA平台WinClient主界面
一.前言 AgileEAS.NET SOA 中间件平台是一款基于基于敏捷并行开发思想和Microsoft .Net构件(组件)开发技术而构建的一个快速开发应用平台.用于帮助中小型软件企业建立一条适合市 ...
- 关于action和category的认知区别
在我的了解, action: intent 有一个或多个action,如果过滤规则中能够匹配到其中一个,是可以成功的 category: intent有一个或多个category,过滤规则需要满足对应 ...