'delete tableViewCell method' 内的 UIAlertController-action

UIAlertController-action inside 'delete tableViewCell method'

很抱歉,如果这看起来像是重复的,我在这里看到了类似的问题,我只是无法得到他们的答案:

我有一个包含用户创建的领域对象列表的 tableView。我当前的删除功能如下所示(并且工作正常):

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let realm = try! Realm()
        try! realm.write {
            realm.delete(allDiaries[indexPath.row])
        }
        tableView.deleteRows(at: [indexPath], with: .fade)
}
}

我的问题是我不希望出现“您确定不想删除吗?”- 带有两个选项的弹出窗口; "Yes, delete!" & "No thanks."

当我滑动删除单元格或按下删除按钮时,我可以轻松创建 UIAlertController 并让它显示它 - 但愚蠢的是,单元格在删除按钮的第二秒被删除被按下,只有在那之后,(完全没用!) UIAlertController 弹出 - 无论你按下什么,它都会消失并且单元格被删除。 我目前的尝试:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    let alert = UIAlertController(title: "Are you sure you wish to delete diary?", message: "", preferredStyle: .alert)
    let action = UIAlertAction(title: "No thanks!", style: .cancel) 
    let action1 = UIAlertAction(title: "Delete!", style: .destructive)// <-- handler: 'deletefunction'
    alert.addAction(action)
    alert.addAction(action1)

    if editingStyle == .delete {

        present(alert, animated: true)

        let realm = try! Realm()
        try! realm.write {
            realm.delete(allDiaries[indexPath.row])
        }
        tableView.deleteRows(at: [indexPath], with: .fade)
    }
}
}

很明显,我不想将 'delete-tableViewCell-method' 移到 "Action1"(弹出警报中的删除按钮!)内并远离删除按钮本身。 <-- 这应该只显示弹出警报!

我想我应该在处理程序中包含这个 'delete-tableViewCell-method' - 但我无法将方法移出此 tableView 方法来创建处理程序,而不会 xCode 抱怨。那么如何创建处理程序并正确插入它呢?

谢谢! :)

喜欢

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

 if editingStyle == .delete {
let alert = UIAlertController(title: "Are you sure you wish to delete diary?", message: "", preferredStyle: .alert)
let action = UIAlertAction(title: "No thanks!", style: .cancel) 
let action1 = UIAlertAction(title: "Delete!", style: .destructive, handler: { _ in
         let realm = try! Realm()
    try! realm.write {
        realm.delete(allDiaries[indexPath.row])
    }
    tableView.deleteRows(at: [indexPath], with: .fade)
    })
alert.addAction(action)
alert.addAction(action1)
    present(alert, animated: true)
}
}

您可以简单地使用闭包来 return 的值是否可以从一个单独的方法中选择。检查下面的代码。

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        self.showDeleteCellAlert { (boolValue) in
            if boolValue {
                tableView.deleteRows(at: [indexPath], with: .fade)
            }
        }
    }
}

private func showDeleteCellAlert(sucess: @escaping ((Bool) -> Void)) {
    let alert = UIAlertController(title: "Are you sure you wish to delete diary?", message: "", preferredStyle: .alert)
    let action = UIAlertAction(title: "No thanks!", style: .cancel) { (_) in
        sucess(false)
    }
    let action1 = UIAlertAction(title: "Delete!", style: .cancel) { (_) in
        sucess(true)
    }
    alert.addAction(action)
    alert.addAction(action1)
    self.present(alert, animated: true, completion: nil)
}