我的表格 View 没有刷新。它保留已删除数据的旧值 并且可以显示新的附加值。 当我关闭应用程序并再次打开它时,它只会刷新(删除已删除的值)。
这是我的代码
private func observeChannels() {
let userEmail = Auth.auth().currentUser?.email
channelRefHandle = channelRef.observe(.childChanged, with: { (snapshot) -> Void in
let channelData = snapshot.value as! Dictionary<String, AnyObject>
let id = snapshot.key
let groupimage = channelData["groupImage"] as? String!
let descc = channelData["desc"] as? String!
let groupCountvar = channelData["Members"]?.count
let groupTasksVar = channelData["Tasks"]?.count
if let name = channelData["name"] as! String!, name.characters.count > 0 {
//members snapshot
if let childSnapshot = snapshot.childSnapshot(forPath: "Members") as? DataSnapshot{
if let membersDictionary = childSnapshot.value as? [String:AnyObject] , membersDictionary.count > 0{
for membersDictionary in membersDictionary {
if userEmail == membersDictionary.value as? String {
self.groups.append(Group(id: id,name: name, createdBy: (userEmail)!, desc: (descc)!, groupImage: (groupimage)!, groupCount: ("\(groupCountvar!)") , groupTasksCount: ("\(groupCountvar!)")))
print(membersDictionary.value)
}
}
}
self.tableView.reloadData()
}}else {
print("Error!")
self.tableView.reloadData()
}
})}
数据源/委托(delegate)
override func numberOfSections(in tableView: UITableView) -> Int {
return 2 }
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let currentSection: Section = Section(rawValue: section) {
switch currentSection {
case .createNewChannelSection:
return 0
case .currentChannelsSection:
return groups.count
}
} else {
return 0 }
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75.0 }
//tableView
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExistingChannel", for: indexPath) as! GroupTableViewCell
if (indexPath as NSIndexPath).section == Section.currentChannelsSection.rawValue {
cell.goupName?.text = groups[(indexPath as NSIndexPath).row].name
cell.groupDesc?.text = groups[(indexPath as NSIndexPath).row].desc
cell.groupimg?.image = UIImage(named: groups[(indexPath as NSIndexPath).row].groupImage)
cell.numbMembLbl?.text = groups[(indexPath as NSIndexPath).row].groupCount
cell.taskNumbLbl?.text = groups[(indexPath as NSIndexPath).row].groupTasksCount
}
return cell }
// MARK: UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if (indexPath as NSIndexPath).section == Section.currentChannelsSection.rawValue {
let channel = groups[(indexPath as NSIndexPath).row]
self.performSegue(withIdentifier: "ShowChannel", sender: channel)
} }
这是我的 TableView ,我不知道为什么删除/删除没有观察到。
最佳答案
您正在观察节点的 .childChanged 事件。
这意味着闭包中的代码只会在子项发生更改时触发。
FIRDataEventTypeChildChanged - Listen for changes to the items in a list. This event is triggered any time a child node is modified. This includes any modifications to descendants of the child node. The snapshot passed to the event listener contains the updated data for the child.
所以这意味着闭包中的代码只会在更改子项时调用,而不会在添加或删除子项时调用。
您需要为 .childAdded 事件和 .childRemoved 事件添加额外的观察者来处理这些情况。当您收到 childAdded 事件时,将该快照数据添加到您的数组中,同样当您收到 childRemoved 事件时,将其从数组中删除。
请记住在所有情况下都重新加载您的 TableView ,以便 UI 使用新数据进行更新。
我个人会更改这一行以使其更具可读性
for membersDictionary in membersDictionary {
到
for member in membersDictionary {
但这只是一种风格。
关于ios - tableview 不刷新已删除的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47805641/