删除单元格后刷新tableView

我有一个tableView,其中包含从Firebase填充的数据,当我单击删除按钮时,数据将从Firebase中删除但它仍保留在我的应用程序中,并且它不会从tableView中删除数据,直到我关闭应用程序并重新打开它。 以下是我设置删除function的方法:

func deletePost() { let uid = FIRAuth.auth()!.currentUser!.uid let storage = FIRStorage.storage().reference(forURL: "gs://gsignme-14416.appspot.com") FIRDatabase.database().reference().child("posts").child(uid).observe(.childAdded, with: { (snapshot) in let indexPath = self.selectedIndex let post = self.posts[(indexPath?.row)!] as! [String: AnyObject] self.key = post["postID"] as? String self.itemsRef = FIRDatabase.database().reference().child("posts").child(uid).child(self.key!) // Remove the post from the DB FIRDatabase.database().reference().child("books").child(self.key!).removeValue { error in if error != nil { print("error \(error)") } } }) self.TableView.reloadData() } 

这是委托和数据源:

  func numberOfSections(in tableView: UITableView) -> Int { // #warning Incomplete implementation, return the number of sections return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { posts.count } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { self.selectedIndex = indexPath self.didExpandCell() if isExpanded && self.selectedIndex == indexPath{ print(indexPath) } else{ }} func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { if isExpanded && self.selectedIndex == indexPath{ return 300 } return 126 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cells", for: indexPath) as! ProfileTableViewCell //Configure the cell let post = self.posts[indexPath.row] as! [String: AnyObject] cell.Title.text = post["title"] as? String cell.Author.text = post["Author"] as? String cell.ISBN10.text = post["ISBN10"] as? String return cell } 

我试图在函数的末尾添加tableview.reloaddata,但这没有帮助。 我究竟做错了什么?

posts数组中删除你的对象,然后在主队列中重新加载tableView ,你从firebase中删除你的对象。

检查以下代码:

 func deletePost() { let uid = FIRAuth.auth()!.currentUser!.uid let storage = FIRStorage.storage().reference(forURL: "gs://gsignme-14416.appspot.com") FIRDatabase.database().reference().child("posts").child(uid).observe(.childAdded, with: { (snapshot) in let indexPath = self.selectedIndex let post = self.posts[(indexPath?.row)!] as! [String: AnyObject] self.key = post["postID"] as? String self.itemsRef = FIRDatabase.database().reference().child("posts").child(uid).child(self.key!) // Remove the post from the DB FIRDatabase.database().reference().child("books").child(self.key!).removeValue { error in if error != nil { print("error \(error)") } else { //Here remove your object from table array self.posts.remove(at: indexPath?.row) //Reload your tableview in main queue DispatchQueue.main.async{ self.TableView.reloadData() } } } }) } 

没有测试过,所以如果您仍然遇到上述代码问题请告诉我。

  func deletePost() { let uid = FIRAuth.auth()!.currentUser!.uid let storage = FIRStorage.storage().reference(forURL: "gs://gsignme-14416.appspot.com") FIRDatabase.database().reference().child("posts").child(uid).observe(.childAdded, with: { (snapshot) in let indexPath = self.selectedIndex let post = self.posts[(indexPath?.row)!] as! [String: AnyObject] self.key = post["postID"] as? String self.itemsRef = FIRDatabase.database().reference().child("posts").child(uid).child(self.key!) // Remove the post from the DB FIRDatabase.database().reference().child("books").child(self.key!).removeValue { error in if error != nil { print("error \(error)") return // use return here or use if else } // no error has occurred,hence move on to remove the post from the array self.posts.remove(at: indexPath.row) } }) DispatchQueue.main.async { self.TableView.reloadData() } } 

从arrays中删除已删除的对象。 您的表格视图使用posts数组填充,而不是来自Firebase对象本身。 当您单击删除按钮时,数据将从Firebase中删除,但它仍保留在您的应用程序中,因为您尚未从arrays中删除该对象,即posts这是为什么它不会从tableView中删除数据,直到您关闭应用程序并重新打开它。

您需要从posts数组中删除已删除的对象并重新加载这些行以获得效果。

  self.posts.remove(at: indexPath.row) 

然后重新加载该特定行本身。

  self.tableView.beginUpdates tableView.reloadRows(at: [indexPath], with: .top) self.tableView.endUpdates; 

在你的情况下

  FIRDatabase.database().reference().child("books").child(self.key!). removeValue { error in if error != nil { print("error \(error)") } else{ self.posts.remove(at: indexPath.row) self.tableView.beginUpdates tableView.reloadRows(at: [indexPath], with: .top) self.tableView.endUpdates; } } }) 

希望能帮助到你。 快乐编码!!