简体   繁体   English

Firebase iOS Swift:删除子项后更新 TableView

[英]Firebase iOS Swift: Updating TableView After Removing A Child

I have a tableview that shows children of a reference.我有一个表格视图,显示参考的孩子。 How can I update the tableview when a child is removed?移除孩子后如何更新 tableview? The child can be removed from other devices and I want the tableview updates automatically.可以从其他设备中删除孩子,我希望 tableview 自动更新。 I know there is observe .childRemoved but not quit sure how to use it to update the tableview.我知道有observe .childRemoved 但不确定如何使用它来更新tableview。

I used observe childRemoved.我使用了observe childRemoved。 Then in the block, I emptied the array, reloaded the tableview, and called ref.observe .childAdded again.然后在块中,我清空数组,重新加载 tableview,并再次调用 ref.observe .childAdded。

ref.observe(.childRemoved, with: {(removedData) in 
            array.removeAll()
            tableView.reloadData()
            fetchUserRequests()
        })

By using: self.tableView.reloadData()通过使用: self.tableView.reloadData()

eg From an old project where I was playing with a sync'd list.例如,来自我正在玩同步列表的旧项目。

self.ref.child("familys").child(currentUserData.family).child("list").observe(.value, with: { (DataSnapshot) in

        if DataSnapshot.hasChildren() == false{
            print("No list")
        }

        else{
            self.list = DataSnapshot.value as! [String]
        }
        self.tableView.reloadData()
}

Everytime something below the level of "shoppinglist" updates, this observe block of code is called.每次低于“购物清单”级别的内容更新时,都会调用此观察代码块。 self.tableView.reloadData() recalls all the functions such as self.tableView.reloadData() 调用所有函数,如

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return(list.count)  
}

Hope this helps.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM