【问题标题】:How to remove a specific key from Firebase in Swift 3如何在 Swift 3 中从 Firebase 中删除特定密钥
【发布时间】:2017-11-13 05:10:26
【问题描述】:

如何获取标记的密钥,然后将其从 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)
        }
    }

【问题讨论】:

  • 我认为您只需要在从源数组中删除一个项目后更新您的表

标签: swift uitableview firebase firebase-realtime-database


【解决方案1】:

要删除,您只需将值设置为 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]
}

【讨论】:

    【解决方案2】:

    您可以从 Firebase 中删除任何数据,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-30
      • 2019-09-12
      • 1970-01-01
      • 2011-10-07
      • 1970-01-01
      相关资源
      最近更新 更多