ios tableview使用侧滑删除操作

ios 8 以前使用的方法:


//设Cell可编辑

- (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath {

    returnYES;

}

//定义样式

- (UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath {

    returnUITableViewCellEditingStyleDelete;

}

//修改按钮文字

- (NSString*)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexPath {

    return@"删除";

}

//删除相应方法

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath {

//在这里实现删除操作

}

ios 8 到 ios11使用如下方法:

//ios 11之前的方法- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewRowAction *a1 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"置顶" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        // 按钮被按了,会调用block里面的代码:

        NSLog(@"置顶");


    }];

    UITableViewRowAction *a2 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"删除" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        // 按钮被按了,会调用block里面的代码:

        NSLog(@"删除");


    }];

    UITableViewRowAction *a3 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        // 按钮被按了,会调用block里面的代码:

        NSLog(@"收藏");

    }];

    a3.backgroundColor = [UIColor greenColor];

    return @[a1, a2, a3];

}

 ios11之后使用的方法

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{

    UIContextualAction *deleteAction = [UIContextualAction

                                        contextualActionWithStyle:UIContextualActionStyleDestructive

                                        title:@"删除"

                                        handler:^(UIContextualAction * _Nonnull action,

                                                  __kindof UIView * _Nonnull sourceView,

                                                  void (^ _Nonnull completionHandler)(BOOL))

                                        {


//                                            [self.tableView setEditing:NO animated:YES];  // 这句很重要,退出编辑模式,隐藏左滑菜单

//                                            [self.arr removeObjectAtIndex:indexPath.row];

//                                            [_table deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationMiddle];

//                                            [_table reloadData];

                                            completionHandler(true);

                                        }];


    deleteAction.backgroundColor = [UIColor grayColor];

    UISwipeActionsConfiguration *actions = [UISwipeActionsConfiguration configurationWithActions:@[deleteAction]];

    actions.performsFirstActionWithFullSwipe = NO;


    return actions;

}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 10,958评论 3 3
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 1,858评论 1 4
  • 1.tableview cell的简单侧滑删除 其实UITableView提供了侧滑删除的方法,我只是怕忘记怎么写...
    开局四带二阅读 235评论 0 0
  • 每次遇见你我都很开心,而当我刚要不开心时就又遇见你了。说实话我不知道自己这样可以坚持多久,但是我想说,只要我想起你...
    感情素质阅读 197评论 0 1
  • 今天是十一的第二天,本来和相依为命的室友去发单,带队的人让我们那一百多人去另一个地方集合,然后那个地方我以前干兼职...
    走路要自信阅读 194评论 0 2

友情链接更多精彩内容