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

标签 ios swift uitableview uiswipeactionsconfiguration

我使用

添加了滑动删除功能
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() - 滑动菜单被关闭,但没有动画效果

最佳答案

您需要使用结果调用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));

关于ios - 如何在取消时关闭滑动操作上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55112175/

相关文章:

ios - 3D Touch UICollectionView insideUITableviewCell

ios - 更改按钮的标题

ios - 如何以编程方式设置 TableView(而不是节)的变量 header ?

ios - 如何在 Swift 2.0 中获得 UIView 的正确大小或边界?

ios - 快速将本地化字符串添加到 UITextView

ios - 如何将 cocoapods 添加到现有项目?

ios - 修改IAP价格等级并发布新版本应用程序

Swift Combine - 观察 N 个对象数组中对象的属性并与其他属性合并

ios - 在 iOS 5.1 上滚动时 UITableView 中的内存泄漏

ios - 试图从对象 [0] 中插入 nil 对象?