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

时间:2018-05-06 11:19:51

标签: ios swift firebase firebase-realtime-database

这是在ios上

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

    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()

}

}

3 个答案:

答案 0 :(得分:1)

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()
    }
}

答案 1 :(得分:0)

您必须使用帖子ID

删除
long totalSeconds = Duration.between(dt1, dt2).getSeconds();
for (long i = 0 ; i < totalSeconds * 1000 ; i++) {
    System.out.println(dt1.plus(i, ChronoUnit.MILLIS).format(formatter));
}

答案 2 :(得分:0)

首先在帖子模型中保存帖子的id, 您可以通过

获取任何对象的密钥(id)
let postID = postSnapshot.key  //if you looping the array of snapshot

之后,您可以在tableView Delegate方法

中获得相同的帖子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()
 }
}