【问题标题】:How to dismiss swipe action context menu on cancel如何在取消时关闭滑动操作上下文菜单
【发布时间】:2019-03-12 00:05:30
【问题描述】:

我添加了使用

的滑动删除功能
override func tableView(_ tableView: UITableView, 
        trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) 
         -> UISwipeActionsConfiguration?

我正在调用带有解除/确认操作的警报。关闭后,我想关闭滑动操作菜单。

    override func tableView(_ tableView: UITableView,
                            trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

        let deleteClosure:UIContextualAction.Handler = {
            (action:UIContextualAction,
            view:UIView,
            completionHandler:(Bool) -> Void) in
            let alert = self.confirmDeletion({ _ in

                let row = indexPath.row;
                let removed = self.logList.remove(at: row);
                self.deleteLogEntry(removed);
                //
                tableView.reloadData();
                //

            }, declineBlock: {_ in
                tableView.reloadData();// this hides it but w/o animation
            });
            DispatchQueue.main.async {
                self.present(alert, animated: true, completion: nil);
            }
        }
        let deleteAction = UIContextualAction(style: .destructive, title: "Delete", handler: deleteClosure);

        return UISwipeActionsConfiguration(actions: [deleteAction]);
    }

到目前为止,我发现重新加载表格数据会关闭尾随滑动菜单,但没有动画,这看起来很奇怪。有没有办法通过动画告诉它关闭?

** 关于重复的注意事项** 标记为重复的问题是指在删除行后解雇未按预期工作,但此处并非如此。当用户取消删除并且未删除行时,我想关闭尾随滑动。


我已经尝试过的:

  • tableView.setEditing(false, animated: true); - 没有明显区别
  • tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.automatic); - 行随着动画向右移动,但按钮重叠
  • tableView.resignFirstResponder(); - 没有明显差异(我必须尝试)
  • tableView.reloadTable() - 滑动菜单被关闭但没有动画效果

【问题讨论】:

  • 我没有使用过 UIContextualAction,但是使用旧版 UITableViewRowAction 我会调用 tableView.setEditing(false, animated:true)
  • 它不起作用,我试过了,我会用我试过的和结果(失败)更新问题。
  • 正如@rmaddy 指出的那样,这似乎是重复的。请参阅链接问题中的第二个答案。在用户取消的情况下,应使用 false 调用完成处理程序(如果未取消删除,则应使用 true 调用)。
  • 你们中的任何人至少在谈论重复之前尝试过这个吗?一旦用户单击删除,使用 false 调用完成处理程序将关闭滑动菜单,BEFORE 警报将要求确认。就像我反复说过的那样,这不是同一个问题。
  • 您似乎无法成功提示用户确认删除,并且Apple似乎没有在Notes应用程序中实现这一点,所以我会这样做。

标签: ios swift uitableview uiswipeactionsconfiguration


【解决方案1】:

您需要使用结果调用 completionHandler 来关闭滑动操作。这是关于 UIContextualActionHandler 的帮助;

// call the completionHandler to reset the context to its normal state (e.g. when swiping, resets to unswiped state)
// pass YES to the completionHandler if the action was actually performed, to show a visual indication of the successful completion
typedef void (^UIContextualActionHandler)(UIContextualAction *action, __kindof UIView *sourceView, void(^completionHandler)(BOOL actionPerformed));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多