用户使用滑动手势编辑时如何离开 UITableViewCellEditingStyleDelete 模式 [英] How to leave UITableViewCellEditingStyleDelete mode when user edit it with swipe gesture

查看:70
本文介绍了用户使用滑动手势编辑时如何离开 UITableViewCellEditingStyleDelete 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在用户点击删除按钮后退出删除模式.我想显示一些活动指示器并在我实际删除单元之前等待服务器响应删除操作(或者如果服务器没有响应则不删除).我想从委托方法执行的这个操作:

I need to leave delete mode after user tapped Delete button. I want to show some activity indicator and wait the server response on delete action before I actually remove the cell (or not if the server does not respond). This action I want to perform from delegate method:

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

我该怎么做?

推荐答案

要隐藏删除"按钮,您需要使用 setEditing:animated: 方法.

To hide the "Delete"-Button, you need to use the setEditing:animated: method.

但是,您对 tableView:commitEditingStyle:forRowAtIndexPath: 的实现需要稍微延迟执行此操作.参考中的注释 Table View Programming Guide for iOS 下图 7-1 说明:

However, your implementation of tableView:commitEditingStyle:forRowAtIndexPath: needs to execute this with a slight delay. The note in the reference Table View Programming Guide for iOS underneath Figure 7-1 states:

注意:数据源不应在 tableView:commitEditingStyle:forRowAtIndexPath: 的实现中调用 setEditing:animated:.如果由于某种原因必须这样做,它应该在延迟后使用 performSelector:withObject:afterDelay: 方法调用它.

Note: The data source should not call setEditing:animated: from within its implementation of tableView:commitEditingStyle:forRowAtIndexPath:. If for some reason it must, it should invoke it after a delay by using the performSelector:withObject:afterDelay: method.

所以你的实现可能是这样的:

So your implementation could be like this:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // remove delete button only after short delay
        [self performSelector:@selector(hideDeleteButton:) withObject:nil afterDelay:0.1];
    }
}

- (void)hideDeleteButton:(id)obj
{
    [self.tableView setEditing:NO animated:YES];
}

当用户按下删除按钮时,上面的代码使删除按钮在 0.1 秒后滑离.当然,您随后需要防止它在等待操作完成时返回编辑模式.为此,您可以覆盖 UITableViewDataSource 方法 tableView:canEditRowAtIndexPath: 以防止在等待时再次编辑同一单元格或整个表格.

The above code makes the Delete-Button slide away after 0.1 seconds when the user presses it. You will then, of course, need to prevent it going back into the editing mode while waiting for the action to complete. For that, you can override the UITableViewDataSource method tableView:canEditRowAtIndexPath: to prevent the same cell or the whole table to be edited again while waiting.

这篇关于用户使用滑动手势编辑时如何离开 UITableViewCellEditingStyleDelete 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆