IOS,滑动删除单元格时从firebase数据库中删除数据

IOS,滑动删除单元格时从firebase数据库中删除数据,ios,swift,firebase,firebase-realtime-database,Ios,Swift,Firebase,Firebase Realtime Database,这是在ios上 我试图从firebase数据库中删除数据,当时我从tableview中滑动删除单元格。下面的代码将删除所有POST数据,而不是我只想从该表的一个单元格中删除的数据 func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .del

这是在ios上

我试图从firebase数据库中删除数据,当时我从tableview中滑动删除单元格。下面的代码将删除所有POST数据,而不是我只想从该表的一个单元格中删除的数据

    func tableView(_ tableView: UITableView, commit editingStyle: 
    UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        self.postData.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
        Database.database().reference().child("Posts").removeValue()

}

}

您必须使用post id删除

let postID = // set id
Database.database().reference().child("Posts").child("\(postID)").removeValue()

首先在post模型中保存post的id, 您可以通过以下方式获取任何对象的密钥(id)

let postID = postSnapshot.key  //if you looping the array of snapshot
之后,您可以在tableView委托方法中获得相同的post ID

  func tableView(_ tableView: UITableView, commit editingStyle: 
UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
    let postID = postData[indexPath.row].postKey //Assuming that you saving postID as a string.

    self.postData.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .fade)
    Database.database().reference().child("Posts").child(postID).removeValue()
 }
}
然后


我需要设置表id吗?但如果在firebase中删除对整个表的引用,则必须选择对应于单元格索引的子项,并在POST下自动为所有内容设置id,以便在ios应用程序上刷新表时列出我表上的所有内容,当我要删除一个单元格时,我希望数据库中的特定单元格被删除。当你从firebase获取数据时,解析每个post的postId键,并用它初始化模型数组中的每个对象,这样你就可以使用indexPath.row访问它,并删除相应的post。你必须更深入一步(postsId)删除所选的帖子数据。请附上firebase结构的屏幕截图。
use like this 
   **to set like this**
    self.postkey.append(snapshot.key)
func tableView(_ tableView: UITableView, commit editingStyle:
    UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        let postID =  self.postkey.remove(at: indexPath.row)
        self.myList.remove(at: indexPath.row)
        self.myTableView.reloadData()
        Database.database().reference().child("List").child(postID).removeValue()
    }
}