首页 > 解决方案 > 删除事件后表格视图中的奇怪行为

问题描述

我正在开发一个让用户注册志愿活动的应用程序,目前正在努力让他们查看自己的活动并取消注册。

在此处输入图像描述

这是用户要求查看他们的事件时看到的屏幕,在这种情况下,这两个事件被称为“ok”和“that”(我刚刚创建了一些随机测试事件)。当您单击一个时,您会看到以下屏幕:

在此处输入图像描述

当您单击取消注册时,该事件将从您的已注册事件中删除,但表视图现在有两个相同的事件。

在此处输入图像描述

当我点击返回,然后回到表格视图时,一切正常,在这种情况下只显示“ok”事件,因为另一个被删除了。这是表格视图布局的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    yourArray = []
    actualEvents = []
    let id = Auth.auth().currentUser?.uid
    Database.database().reference().child("users").child(id!).child("registeredEvents").observe(.value) { snapshot in
          let children = snapshot.children
             while let rest = children.nextObject() as? DataSnapshot, let value = rest.value {
                print(value)
                  self.yourArray.append(value as! String)
              }
               Database.database().reference().child("Events").observe(.value) { (data) in
                    let events = data.value as! [String:[String:Any]]
                    for(_,value) in events{
                        if(self.yourArray.contains(value["EventName"]! as! String)){
                            self.actualEvents.append(PersonalEvents(evName: value["EventName"]! as! String, evDesc: value["EventDescription"]! as! String, evStartDate: value["start time"]! as! String, evEndDate: value["end time"] as! String, evNumPeople: value["NumberOfPeople"]! as! Int, evNumRegistered: value["currentPeople"] as! Int))
                           }
                        }
                        print("Actual events array " + "\(self.actualEvents)")
                  }
            self.tblEvents.reloadData()
        }
        print(yourArray)
        self.tblEvents.dataSource = self
        self.tblEvents.delegate = self
    }

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "productstable", for: indexPath)
    cell.textLabel?.text = self.yourArray[indexPath.row]
    return cell
}

和取消注册按钮(这是在不同的视图控制器中,因为信息显示在不同的视图中的事件):

 @IBAction func didUnregister(_ sender: Any) {
    let id = Auth.auth().currentUser?.uid
    let ref = Database.database().reference()
    ref.child("users").child(id!).child("registeredEvents").child(personalEventInfo!.eventName!).removeValue { error,arg  in
      if error != nil {
          print("error \(error)")
      }
    }
    let event = ref.child("Events")
    event.child(personalEventInfo!.eventName!).observeSingleEvent(of: .value) { (snapshot) in
        let value = snapshot.value as! NSDictionary
        var currentPeople = value["currentPeople"] as! Int
        currentPeople = currentPeople - 1
    Database.database().reference().child("Events").child(self.personalEventInfo!.eventName!).child("currentPeople").setValue(currentPeople)
    }
}

请让我知道这是否令人困惑,但如果不是,请让我知道为什么会发生这种情况,以及我能做些什么来解决它。

标签: iosswiftfirebase-realtime-databasetableviewswift4

解决方案


  • 在将数据附加到其中之前,请删除 Array 中的项目。
  • 改进:你需要在这些闭包中使用 [weak self] 来保护你的记忆。

您可以尝试以下方法:

 Database.database().reference().child("users").child(id!).child("registeredEvents").observe(.value) { [weak self] snapshot in

    let children = snapshot.children
    self?.yourArray.removeAll()
         while let rest = children.nextObject() as? DataSnapshot, let value = rest.value {
            print(value)
              self?.yourArray.append(value as! String)
          }

    Database.database().reference().child("Events").observe(.value) { [weak self] (data) in
                let events = data.value as! [String:[String:Any]]

                self?.actualEvents.removeAll()
                for(_,value) in events{
                    if(self?.yourArray.contains(value["EventName"]! as! String)){
                        self?.actualEvents.append(PersonalEvents(evName: value["EventName"]! as! String, evDesc: value["EventDescription"]! as! String, evStartDate: value["start time"]! as! String, evEndDate: value["end time"] as! String, evNumPeople: value["NumberOfPeople"]! as! Int, evNumRegistered: value["currentPeople"] as! Int))
                       }
                    }
                    print("Actual events array " + "\(self?.actualEvents)")
              }
        self?.tblEvents.reloadData()
    }

推荐阅读