'删除tableViewCell方法'内的UIAlertController-action

时间:2018-11-09 07:37:23

标签: ios swift tableview uialertcontroller delete-row

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

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

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)
}
}

我的问题是,我不想出现“您确定要删除吗?”-弹出窗口,其中有两个选项; “是的,删除!” &“不,谢谢。”

当我滑动以删除单元格或按Delete键时,我可以轻松创建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-button!)内,并远离Delete-button本身。 <-仅应显示弹出警报!

我猜我应该在处理程序中使用这个'delete-tableViewCell-method'-但是我只是不能在没有xCode抱怨的情况下将该方法移出tableView-method来创建处理程序。那么如何创建处理程序并正确插入呢?

谢谢! :)

2 个答案:

答案 0 :(得分:1)

喜欢

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)
}
}

答案 1 :(得分:0)

您可以简单地使用闭包从一个单独的方法返回是否可删除的值。检查下面的代码。

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)
}