tabelView右滑选择进行删除

如何使用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

方法。

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if(indexPath.row==0){
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");

        }];

    }
    else if (indexPath.row==1){
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");

        }];

        // 添加一个修改按钮
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了修改");
        }];
        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

    }
    else if (indexPath.row==2){
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");

        }];

        // 添加一个修改按钮
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了修改");
        }];
        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

        // 添加一个发送按钮

        sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了发送");
        }];
        sanRowAction.backgroundColor=[UIColor orangeColor];

    }
    else{
        // 添加一个删除按钮
        deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了删除");

        }];

        // 添加一个修改按钮
        moreRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"修改" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了修改");
        }];
        moreRowAction.backgroundEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];

        // 添加一个发送按钮

       sanRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"发送" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了发送");
        }];
        sanRowAction.backgroundColor=[UIColor orangeColor];
        // 添加一个发送按钮

        OK = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"OK键" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
            NSLog(@"点击了OK");
        }];
        OK.backgroundColor=[UIColor purpleColor];

    }

    // 将设置好的按钮放到数组中返回
    if (indexPath.row==0) {
        return @[deleteRowAction];

    }else if (indexPath.row==1){
        return @[deleteRowAction,moreRowAction];

    }else if(indexPath.row==2){
        return @[deleteRowAction,moreRowAction,sanRowAction];

    }else if(indexPath.row==3){
        return @[deleteRowAction,moreRowAction,sanRowAction,OK];

    }
    return nil;
}
时间: 2024-08-29 13:59:55

tabelView右滑选择进行删除的相关文章

iOS tableView右滑显示选择

如何使用UITableViewRowAction实现右滑选择呢? 1.在iOS8以前,我们实现tableview中滑动显示删除,置顶,更多等等的按钮时,都需要自己去实现,在iOS8中系统已经写好了,只要一个代理方法和一个类就行了 2.iOS8的协议对了一个方法,返回值是数组的tableview:editActionForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableviewRowAction 3.在UITableviewRo

Android SwipeToDismiss:左滑/右滑删除ListView条目Item

?? <Android SwipeToDismiss:左右滑动删除ListView条目Item> Android的SwipeToDismiss是github上一个第三方开源框架(github上的项目链接地址:https://github.com/romannurik/Android-SwipeToDismiss ).该开源项目旨在:使得一个ListView的item在用户的手指在屏幕上左滑或者右滑时候,删除当前的这个ListView Item. 此种特效在新版的Android中应用不少.比方在

利用swipelistview完成qq聊天列表右滑删除功能

转载请注明出处:http://blog.csdn.net/harryweasley/article/details/41413547 前言:前段时间,由于自己比较悠闲,没有什么工作上的事情所做,所以当时就想,何不做一下qq聊天列表的item右滑出现后面的视图,然后我就开始着手自己做,之后无意间发现了可以利用一个github开源项目swiplistview完成,但是当我查看那些swiplistview的相关文档时,很不全面,github官网上下载的和之前他们写的文档已经有所不同,所以在这里我就重新

类似于QQ的右滑删除效果的实现方法

类似于QQ的右滑删除效果的实现方法 原理:删除的div在窗口的外面,用户看不到,用户右滑,显示次div ? 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 6

iOS笔记【单元格右滑手势】 2017-09-09

// 响应单元格右滑手势 override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let actionShare = UITableViewRowAction(style: .normal, title: "分享") { (_,IndexPath) in // 定义滑动菜单样式和标题 let ac

Cell右滑 多个编辑选项栏

简单粗暴,一看就能明白 关于右滑cell,能滑出来两个以上的选项栏,可以如下这么做,但是要注意下面的注意事项,就是关于iOS8前后的问题,注释写的很清楚了.可以直接复制到自己的代码里看的会更明白. 1 //允许cell可以进行编辑 2 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ 3 return YES; 4 } 5 //cell的编辑类型 6 - (UITa

截获导航控制器系统返回按钮的点击pop及右滑pop事件

前几天看了@栾小布的一篇文章:Custom backBarButtonItem,在跟着做的时候我又顺便扩展了一些,写此文章的目的是为了总结一下自己所写的东西,方便以后翻看容易,同时也是自己入行iOS一年时间,希望写点东西练练手,还有希望可以分享给大家,希望大家一同讨论,提出宝贵意见以及更简单的实现.总体效果如下: 同时受@J_雨的轻松学习之——IOS利用Runtime自定义控制器POP手势动画一文影响,所以我们将用两种方法实现. 实现思路 每一个navigationController都自带有一

iOS7 NavigationController 右滑手势问题

苹果一直都在人机交互中尽力做到极致,在iOS7中,新增加了一个小小的功能,也就是这个api:self.navigationController.interactivePopGestureRecognizer.enabled = YES; 这个api功能就是在NavigationController堆栈内的UIViewController可以支持右滑手势,也就是不用点击右上角的返回 按钮,轻轻在屏幕左边一滑,屏幕就会返回,随着ios设备屏幕的增大,这个小功能让手指短,拇指大和手残人士看到了福音.

Cell右滑的动作状态

//允许cell可以进行编辑 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{ return YES; } //cell的编辑类型 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPa