簡體   English   中英

刪除單元格Swift Xcode之前的警報控制器

[英]Alert Controller Before Delete Cell Swift Xcode

我想知道如何在刪除單元格之前顯示刪除確認(警報)。 單元格是帶有信息的字符,因此如果用戶錯誤地刪除(滑動)一個單元格將是很糟糕的。

這是允許我刪除行的代碼段。 我正在使用Xcode 10 / Swift

// Delete Rows
override func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let modifyAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Update action ...")
        success(true)

        self.namesArray.remove(at: indexPath.row)
        self.imagesArray.remove(at: indexPath.row)

        self.tableView.deleteRows(at: [indexPath], with: .fade)

    })
    modifyAction.image = UIImage(named: "delete")
    modifyAction.backgroundColor = .purple

    return UISwipeActionsConfiguration(actions: [modifyAction])

}

你可以試試

override func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let modifyAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Update action ...")
        success(true)

         let alertView = UIAlertController(title: "", message: "Are you sure you want to delete the item ? ", preferredStyle: .alert)
         let okAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
             self.namesArray.remove(at: indexPath.row)
             self.imagesArray.remove(at: indexPath.row)     
             self.tableView.deleteRows(at: [indexPath], with: .fade)
        })
        let cancelAction = UIAlertAction(title: "Cancel", style:.cancel, handler: { (alert) in
             print("Cancel")
        })
        alertView.addAction(okAction)
        alertView.addAction(cancelAction)
        self.present(alertView, animated: true, completion: nil)

    })
    modifyAction.image = UIImage(named: "delete")
    modifyAction.backgroundColor = .purple 
    return UISwipeActionsConfiguration(actions: [modifyAction])

}

您可以拉出代碼以將UIAlertController顯示為一個單獨的方法,當用戶按下Delete鍵時可以調用該方法。 這是下面的代碼,確保閉包中包含的所有表示邏輯都被調度到主線程。

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let modifyAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        DispatchQueue.main.async {
            self.showDeleteWarning(for: indexPath)
        }

        success(true)
    })

    modifyAction.image = UIImage(named: "delete")
    modifyAction.backgroundColor = .purple

    return UISwipeActionsConfiguration(actions: [modifyAction])
}

func showDeleteWarning(for indexPath: IndexPath) {
    //Create the alert controller and actions
    let alert = UIAlertController(title: "Warning Title", message: "Warning Message", preferredStyle: .alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { _ in
        DispatchQueue.main.async {
            self.namesArray.remove(at: indexPath.row)
            self.imagesArray.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

    //Add the actions to the alert controller
    alert.addAction(cancelAction)
    alert.addAction(deleteAction)

    //Present the alert controller
    present(alert, animated: true, completion: nil)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM