如何在取消时关闭滑动操作上下文菜单

Luk*_*ela 2 uitableview ios swift uiswipeactionsconfiguration

我添加了滑动删除功能,使用

override func tableView(_ tableView: UITableView, 
        trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) 
         -> UISwipeActionsConfiguration?
Run Code Online (Sandbox Code Playgroud)

我正在通过解除/确认操作发出警报。一旦关闭,我想关闭滑动操作菜单。

    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]);
    }
Run Code Online (Sandbox Code Playgroud)

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

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


我已经尝试过的:

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

小智 5

您需要使用结果调用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));
Run Code Online (Sandbox Code Playgroud)