【发布时间】:2017-07-01 08:12:19
【问题描述】:
我有一个从 Firebase 填充数据的 tableView,当我单击删除按钮时,数据会从 Firebase 中删除,但它仍保留在我的应用程序中,并且在我关闭应用程序并重新打开它之前它不会从 tableView 中删除数据.以下是我设置删除功能的方法:
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 但这没有帮助。我做错了什么?
【问题讨论】:
-
请发布您的 tableview 委托和数据源函数,因为这些信息不足以提供帮助!
-
需要在removeValue的回调中调用reloadData()。
-
@SteffenSchmitz 这也不起作用
-
那你能把控制器的其余部分贴出来吗?尤其是在哪里分配 tableView 委托和委托实现?
-
从
posts数组中删除您的对象,然后在主队列中重新加载您的表视图,然后从 Firebase 中删除您的对象。
标签: ios swift uitableview firebase-realtime-database