gpt4 book ai didi

ios - 删除事件后 TableView 中的奇怪行为

转载 作者:行者123 更新时间:2023-11-28 23:21:36 25 4
gpt4 key购买 nike

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

enter image description here

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

enter image description here

当您点击注销时,该事件将从您注册的事件中删除,但 TableView 现在有两个相同的事件。

enter image description here

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

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
}

取消注册按钮(这是在不同的 View Controller 中,因为有关事件的信息显示在不同的 View 中):

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

如果这令人困惑,请告诉我,如果不是,请告诉我为什么会这样,以及我可以做些什么来解决它。

最佳答案

  • 在将数据追加到数组中之前删除数组中的项目。
  • 改进:您需要通过在这些闭包中使用 [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()
}

关于ios - 删除事件后 TableView 中的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59595890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com