从 UITableView 和 Firebase 中删除单元格



我尝试删除单元格(左滑动(并从Firebase数据库和存储中删除数据。使用此代码:

struct Records {
let title: String
let source: String
var length: String
let recordUrl: String
let username: String
init(dictionary: [String: Any]) {
self.title = dictionary["title"] as? String ?? ""
self.source = dictionary["source"] as? String ?? ""
self.length = dictionary["length"] as? String ?? ""
self.recordUrl = dictionary["recordUrl"] as? String ?? ""
self.username = dictionary["username"] as? String ?? ""
}
}

我有一个变量

var records = [Records]()

尝试删除单元格

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
guard let uid = Auth.auth().currentUser?.uid else { return }
if (editingStyle == .delete) {
records.remove(at: indexPath.item)
tableView.deleteRows(at: [indexPath], with: .fade)
Database.database().reference().child("users").child(uid).child("records").removeValue()
}
}

Firebase JSON 在此处输入图片说明

我需要删除"记录"中的项目(在我的 TableView 中显示为单元格(,但现在使用我的代码删除了整个"记录">

您还需要保存记录的 ID。这样,您可以创建该特定记录的路径。 之后,只需在路径中再添加一个子项即可。

喜欢这个。

Database.database().reference().child("users").child(uid).child("records").child(record.id).removeValue()

有一些新的。可以获取分支"记录"中所有项目的键

guard let uid = Auth.auth().currentUser?.uid else { return }
let ref = Database.database().reference().child("users").child(uid).child("records")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionaries = snapshot.value as? [String: Any] else { return }
let record = self.records[indexPath.item]
dictionaries.forEach({ (key, value) in
print("Key (key)")

但仍然无法获取 ID 以删除当前项目(分支"记录"中的记录(

最新更新