gpt4 book ai didi

ios - 如何从 UItableview 中删除单元格,当它在 onChildRemoved 方法中从 Firebase 中删除时?

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

我正在用来自 Firebase 的数据填充我的 uitableview,当我从 Firebase 中删除数据时,它仍然显示。我不知道在我的 onChildRemoved 方法中放入什么来删除它。我已经搜索了很多次如何做到这一点,但我仍然卡住了!

这是我的代码:

 class class_custom_cell2: UITableViewCell {
@IBOutlet weak var cc: UILabel!

@IBOutlet weak var teacher: UILabel!
@IBOutlet weak var rn: UILabel!

@IBOutlet weak var id: UILabel!


}

class Home: UIViewController , UITableViewDelegate, UITableViewDataSource{
var classes2 = [Class]()
var pass1: String!
var pass2: String!
var pass3: String!
var pass4: String!
@IBOutlet weak var tb_Yourclasses: UITableView!


override func viewDidLoad() {
super.viewDidLoad()

fetchClass2()

}

func fetchClass2(){
let userID = Auth.auth().currentUser!.uid

print(userID)
Database.database().reference().child("Users").child(userID)
.child("Classes").observe(.childAdded, with: { (snapshot) in
print("1",snapshot)
//need to get the child"class_info" from firebase
let class_id = snapshot.value as? String
print("2",class_id)

Database.database().reference().child("Classes").child(class_id!)
.child("class_info").observe(DataEventType.value, with: { (snapshot) in
print("2.5",snapshot)
let postDict = snapshot.value as? [String : AnyObject] ?? [:]


print("3", postDict)
let class_object = Class(dictionary: postDict)
self.classes2.append(class_object)
//self.classes.append(class_object)


DispatchQueue.main.async(execute: {
self.tb_Yourclasses.reloadData()
})


}, withCancel: nil)



}, withCancel: nil)
Database.database().reference().child("Users").child(userID).child("Classes").observe(.childRemoved, with: { (snapshot) in
//idk what to do here
let id = snapshot.value as? String
})

}
@objc func handleCancel() {
dismiss(animated: true, completion: nil)
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//print(classes2)
return classes2.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! class_custom_cell2

let data_class2 = classes2[indexPath.row]
print(data_class2)
cell.cc?.text = data_class2.date_clasname
cell.teacher?.text = data_class2.teacher
cell.rn?.text = data_class2.room_number
cell.id?.text = data_class2.class_id
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)! as! class_custom_cell2
pass1 = currentCell.cc.text

pass2 = currentCell.teacher.text
pass3 = currentCell.rn.text
pass4 = currentCell.id.text
performSegue(withIdentifier: "remove_detail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier=="remove_detail") {
let viewController = segue.destination as! removeClassDetail
viewController.receivedClassName = pass1
viewController.receivedTeacher = pass2

viewController.receiveRoomNumber = pass3
viewController.receiveClassID = pass4

}
}
}

这是数据库结构:

{
"Classes" : {

"-LiGRe3e1YF_HFrHKMgi" : {
"Students" : {
"xfDGLrXF4gP8AJAPtAPc4VMe9y72" : "xfDGLrXF4gP8AJAPtAPc4VMe9y72"
},
"class_info" : {
"date_clasname" : "mm",
"room_number" : "jjhh",
"subject" : "Science",
"teacher" : "mm",
"uid" : "-LiGRe3e1YF_HFrHKMgi"
}
},


"Users" : {
"xfDGLrXF4gP8AJAPtAPc4VMe9y72" : {
"Classes" : {
"-LiGRe3e1YF_HFrHKMgi" : "-LiGRe3e1YF_HFrHKMgi",

},
"User_info" : {
"email" : "c@gmail.com",
"grade" : "2",
"name" : "n",
"uid" : "xfDGLrXF4gP8AJAPtAPc4VMe9y72"
}
}
}
}

谢谢,如果您有任何问题,请告诉我!

最佳答案

希望对你有帮助

Database.database().reference().child("Users").child(userID).child("Classes").observe(.childRemoved, with: { (snapshot) in
let id = snapshot.value as? String
for i in 0..<classes2.count {
if (id == classes2[i].class_id) {
DispatchQueue.main.async(execute: {
classes2.remove(at: i)
self.tb_Yourclasses.reloadData()
})
return
}
}
})

如果你有任何问题请联系我

关于ios - 如何从 UItableview 中删除单元格,当它在 onChildRemoved 方法中从 Firebase 中删除时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275207/

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