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

时间:2018-08-14 21:13:36

标签: swift uitableview animation

我搜索了2小时以上,但没有发现任何有用的信息: 我有一个带有editActions的tableView。当我按下动作时,我会向AlertController展示附加信息/需要确认。

在显示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)
}

1 个答案:

答案 0 :(得分:0)

这是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方法。