如何使用UITableViewRowAction实现右滑选择呢?

1、在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只要一个代理方法和一个类就行了

2、iOS8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction

3、在UITableviewRowAction类。我们可以设置按钮的样式,显示文字、背景色和按钮事件(在block内实现)

4、在代理方法中,我们可以常见多个按钮放到数组中返回,最先放入数组的按钮显示在最右边,最后放入的显示在最左边

5、如果自己设定一个或多个按钮,系统自带的删除按钮就消失了

设置tableView可以编辑

- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

UITableViewRowAction的使用方法:

+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(nullable NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;

重写UITableViewDelegate的

- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath

方法。

  1. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3.  
  4. if(indexPath.row==){
  5. // 添加一个删除按钮
  6. deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  7. NSLog(@"点击了删除");
  8.  
  9. }];
  10.  
  11. }
  12. else if (indexPath.row==){
  13. // 添加一个删除按钮
  14. deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  15. NSLog(@"点击了删除");
  16.  
  17. }];
  18.  
  19. // 添加一个修改按钮
  20. moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  21. NSLog(@"点击了修改");
  22. }];
  23. moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  24.  
  25. }
  26. else if (indexPath.row==){
  27. // 添加一个删除按钮
  28. deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  29. NSLog(@"点击了删除");
  30.  
  31. }];
  32.  
  33. // 添加一个修改按钮
  34. moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  35. NSLog(@"点击了修改");
  36. }];
  37. moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  38.  
  39. // 添加一个发送按钮
  40.  
  41. sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  42. NSLog(@"点击了发送");
  43. }];
  44. sanRowAction.backgroundColor=[UIColor orangeColor];
  45.  
  46. }
  47. else{
  48. // 添加一个删除按钮
  49. deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  50. NSLog(@"点击了删除");
  51.  
  52. }];
  53.  
  54. // 添加一个修改按钮
  55. moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  56. NSLog(@"点击了修改");
  57. }];
  58. moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
  59.  
  60. // 添加一个发送按钮
  61.  
  62. sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  63. NSLog(@"点击了发送");
  64. }];
  65. sanRowAction.backgroundColor=[UIColor orangeColor];
  66. // 添加一个发送按钮
  67.  
  68. OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK键" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  69. NSLog(@"点击了OK");
  70. }];
  71. OK.backgroundColor=[UIColor purpleColor];
  72.  
  73. }
  74.  
  75. // 将设置好的按钮放到数组中返回
  76. if (indexPath.row==) {
  77. return @[deleteRowAction];
  78.  
  79. }else if (indexPath.row==){
  80. return @[deleteRowAction,moreRowAction];
  81.  
  82. }else if(indexPath.row==){
  83. return @[deleteRowAction,moreRowAction,sanRowAction];
  84.  
  85. }else if(indexPath.row==){
  86. return @[deleteRowAction,moreRowAction,sanRowAction,OK];
  87.  
  88. }
  89. return nil;
  90. }

iOS tableView右滑显示选择的更多相关文章

  1. iOS系统右滑返回全局控制方案

    前言 今天有个小需求,在点击导航条上的返回按钮之前要调用某个API,并弹出UIAlertView来显示,根据用户的选项判断是否是返回还是继续留在当前控制器.举个简单的例子,当点击导航条上的左上角返回按 ...

  2. iOS 开发 右滑返回上一级控制器

    #import <objc/runtime.h> @interface UINavigationController (Transition)<UIGestureRecognizer ...

  3. iOS页面右滑返回的实现方法总结

    1.边缘触发的系统方法 ①系统返回按钮 self.navigationController.interactivePopGestureRecognizer.enabled = YES;  ②自定义返回 ...

  4. tabelView右滑选择进行删除

    如何使用UITableViewRowAction实现右滑选择呢? 1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只 ...

  5. iOS开发之自定义导航栏返回按钮右滑返回手势失效的解决

    我相信针对每一个iOS开发者来说~除了根视图控制器外~所有的界面通过导航栏push过去的界面都是可以通过右滑来返回上一个界面~其实~在很多应用和APP中~用户已经习惯了这个功能~然而~作为开发者的我们 ...

  6. iOS开发之--单个页面禁止右滑返回操作

    禁止右滑: if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) ...

  7. iOS 当使用FD_FullscreenPopViewController的时候遇到scrollView右滑手势无法使用的解决

    当我们在ViewController中有scrollView的时候, 可能会遇到右滑无法响应返回手势, 有以下解决办法: 自定义scrollView, 实现该scrollView的以下方法即可: @i ...

  8. Cell右滑 多个编辑选项栏

    简单粗暴,一看就能明白 关于右滑cell,能滑出来两个以上的选项栏,可以如下这么做,但是要注意下面的注意事项,就是关于iOS8前后的问题,注释写的很清楚了.可以直接复制到自己的代码里看的会更明白. / ...

  9. Cell右滑的动作状态

    //允许cell可以进行编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)index ...

随机推荐

  1. Sql Server系列:Select基本语句

    1 T-SQL中SELECT语法结构 <SELECT statement> ::= [WITH <common_table_expression> [,...n]] <q ...

  2. MVC4做网站后台:栏目管理1、添加栏目

    把栏目添加删除跟前台混在一起结构清晰,现在有了后台管理的区域就把后台管理相关的代码分开. 要实现功能: 1.添加栏目 2.删除栏目 3.修改栏目信息 -- 一.开始 1.添加 接口InterfaceC ...

  3. jsp通过易宝方式实现在线支付

    项目下载地址: https://github.com/hjzgg/OnlinePayment 参考:http://blog.csdn.net/jadyer/article/details/738025 ...

  4. 详解 ML2 Core Plugin(I) - 每天5分钟玩转 OpenStack(71)

    我们在 Neutron Server 小节学习到 Core Plugin,其功能是维护数据库中 network, subnet 和 port 的状态,并负责调用相应的 agent 在 network ...

  5. Android自定义View 画弧形,文字,并增加动画效果

    一个简单的Android自定义View的demo,画弧形,文字,开启一个多线程更新ui界面,在子线程更新ui是不允许的,但是View提供了方法,让我们来了解下吧. 1.封装一个抽象的View类   B ...

  6. ZOJ Problem Set - 1240 IBM Minus One

    水题不解释,就是注意下格式,没输出一行字符串记得加一个空白行 #include <stdio.h> #include <string.h> int main() { ; ]; ...

  7. 解读SDN的东西、南北向接口

    北向接口(Northbound Interface)是为厂家或运营商进行接入和管理网络的接口,即向上提供的接口. 南向接口(Southbound Interface)是提供对其他厂家网元的管理功能,支 ...

  8. JavaScript的三种工业化调试方法

    JavaScript的三种工业化玩法 软件工程中任何的语言如果想要写出健壮的代码都需要锋利的工具,当然JavaScript也不例外,很多朋友刚入门的时候往往因为工具选的不对而事半功倍,JavaScri ...

  9. [Asp.net 5] Logging-新日志系统目录

    楼主有个美好的愿望——把asp.net 5所有能看懂的代码一一呈现给大家(比如C++,楼主就看不懂).现在已经做完了依赖注入.多语言.配置文件三部分,比较基础的日志就成为了楼主的下一个目标.下面是楼主 ...

  10. C#获得MAC地址(网卡序列号)代码

    代码如下: //获得网卡序列号 //MAc地址 http://www.cnblogs.com/sosoft/ public string GetMoAddress() { string MoAddre ...