簡體   English   中英

如何在Swift 3中從Firebase中刪除特定密鑰

[英]How to remove a specific key from Firebase in Swift 3

如何獲取標記的鍵,然后將其從UITableView swift 3中刪除?

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

        if editingStyle == .delete {

            self.grocery.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .automatic)
        }
    }

請參考這張圖片。

要刪除,您只需將值設置為nil即可將其從firebase中刪除。 調用remove函數之后,請確保從tableView中刪除該行。

func removeObjectFromFireBase(userKey: String, removeThisGrocery: String) {
    let ref = Database.database().reference()
    let groceryRef = ref.child("Users").child(userKey).child("Grocery")
    groceryRef.child(removeThisObject).setValue(nil)
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .destructive, title: "DELETE") { (rowAction, indexPath) in

        //Call the removeFromFirebase function with the required parameters
        removeObjectFromFirebase(userKey: userKey, removeThisGrocery: groceryKey)
        tableView.deleteRows(at: [indexPath], with: .automatic)
    }

    deleteAction.backgroundColor = UIColor.red
    return [deleteAction, addAction]
}

您可以按以下方式從Firebase中刪除任何數據:

let ref = Database.database().reference()
let groceryRef = ref.child("Users").child(userKey).child("Grocery")
groceryRef.removeValue()

暫無
暫無

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

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