tableview不刷新已删除的数据

问题描述 投票:0回答:3

我的表视图不刷新。它保留已删除数据的旧值,并可显示新添加的值。它只在我关闭应用程序并再次打开它时刷新(删除已删除的值)。

这是我的代码

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

数据源/委托

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

这是我的表视图我不知道为什么删除/删除不遵守。

ios swift uitableview firebase firebase-realtime-database
3个回答
1
投票

您只是在观察.childChanged事件的节点。

这意味着闭包中的代码只会在子项被更改时触发。

来自documentation

FIRDataEventTypeChildChanged - 侦听列表中项目的更改。每次修改子节点时都会触发此事件。这包括对子节点后代的任何修改。传递给事件侦听器的快照包含子级的更新数据。

这意味着只有在更改子项时才会调用闭包中的代码,并且在添加或删除子项时不会调用该代码。

您需要为.childAdded事件和.childRemoved事件添加其他观察器来处理这些情况。当您收到childAdded事件时,将该快照数据添加到您的阵列,同样在收到childRemoved事件时,将其从阵列中删除。

请记住在所有情况下重新加载表视图,以便UI使用新数据进行更新。

我个人会改变这一行,使其更具可读性

for membersDictionary in membersDictionary {

for member in membersDictionary {

但这只是一种风格。


0
投票

尝试使用它来重新加载tableView

DispatchQueue.main.async {
    self.tableView.reloadData()
}

这会有所帮助。


0
投票

你需要这个 :

func tableView(_ tableView: UITableView, commit editingStyle: 
 UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
  if editingStyle == .delete {
  print("Deleted")
  self.yourArrayName.remove(at: indexPath.row)
  self.tableView.deleteRows(at: [indexPath], with: .automatic)
 }
}

它会完成这项工作。

© www.soinside.com 2019 - 2024. All rights reserved.