swift - UITableViewRowAction - 延迟关闭动画直到确认

标签 swift uitableview animation

我用谷歌搜索了两个多小时,没有发现任何有用的东西: 我有一个带有 editActions 的 tableView 。当我按下该操作时,我会显示一个包含附加信息/需要确认的警报 Controller 。

显示alertController后,editActions立即被取消。 我想等待关闭动画,直到我的 alterController 被关闭。

这是我的代码:

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in
        let cell = tableView.cellForRow(at: indexPath) as? DocumentTableViewCell

        self.askUserForDeleteFile() { (shallDelete) in
            if shallDelete {
                self.imageDocumentHandler?.deleteDocumentOfDatabase(self.fetchedResultsController.object(at: indexPath))
            }
        }
    }

    let deselect = UITableViewRowAction(style: .normal, title: "kickout") { (action, indexPath) in
            self.folder?.removeFromDocuments(self.fetchedResultsController.object(at: indexPath))
    }

    deselect.backgroundColor = UIColor(displayP3Red: 247/255, green: 162/255, blue: 180/255, alpha: 1.0)

    return [deselect,delete]
}

func askUserForDeleteFile(completion: @escaping (Bool)->()) {
    let alertController = UIAlertController(title: "delete document?", message: nil, preferredStyle: .actionSheet)

    let deleteAction = UIAlertAction(title: "delete", style: .destructive) { (_) in
        completion(true)
    }
    let cancelAction = UIAlertAction(title: "cancel", style: .cancel) { (_) in
        completion(false)
    }

    alertController.addAction(deleteAction)
    alertController.addAction(cancelAction)

    self.present(alertController, animated: true, completion: nil)
}

最佳答案

这是 editAction 滑动删除的标准行为。但是,如果您的目标是 IOS 11 或更高版本,那么您可以使用新方法来实现您的需求。你可以做这样的事情。

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

    let delete = UIContextualAction.init(style: UIContextualAction.Style.destructive, title: "delete", handler: { (action, view, completion) in

        let cell = tableView.cellForRow(at: indexPath) as? DocumentTableViewCell

        self.askUserForDeleteFile() { (shallDelete) in
            if shallDelete {
                self.imageDocumentHandler?.deleteDocumentOfDatabase(self.fetchedResultsController.object(at: indexPath))
            }
            completion(true)
        }
    })

    let deselect = UIContextualAction.init(style: UIContextualAction.Style.normal, title: "kickout", handler: { (action, view, completion) in
         self.folder?.removeFromDocuments(self.fetchedResultsController.object(at: indexPath))
        completion(true)
    })

    let config = UISwipeActionsConfiguration(actions: [delete, deselect])
    return config
}

除非指定,否则完成将阻止动画被关闭。但是,如果您的目标是 IOS 10,您仍然需要 editAction 方法。

关于swift - UITableViewRowAction - 延迟关闭动画直到确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51849834/

相关文章:

ios - 从 UIImagePickerController() 选取图像后,我尝试显示 UIAlertViewController() 但它没有出现并锁定屏幕

ios - 弹出窗口中的 Swift tableview 不显示数据

iphone - 我如何向分组的 UITableView 添加阴影(如官方 Twitter 应用程序所示)?

ios - UITableView 不填充数据

ios - 如何识别 Xcode UI 测试中的控制中心元素?

ios - 当它已经可见时关闭键盘

javascript - 平滑有问题的 javascript 动画(逻辑问题)

javascript - 香蕉 Sprite js用json数据定制前后动画

带有溢出的 div 的 jquery marquee-like 动画 :hidden and nowrap?

Swift Perfect 不加载图像或 css