[英]How to get index path for key - SWIFT?
我是 Swift 的新手,所以我有 tableview,我需要从 firebase 获取键的索引路径。 单击行时,我想打开一个新的 controller ,其中显示该用户的所有数据(用 Firebase 编写)。
这是我的 didSelectRowAt 代码:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedGuest = guestsList[indexPath.row]
let controller = self.storyboard?.instantiateViewController(identifier: "GuestDetail") as! GuestDetailsViewController
refGuests = Database.database().reference().child("userInfo").child(uid!).child("guests")
refGuests.observeSingleEvent(of: .value) { (snapshot) in
for snap in snapshot.children.allObjects as![DataSnapshot] {
let uid = snap.key
let guestD = snap.value as! [String:AnyObject]
let guestN = guestD["guestPhoneNumber"] as! String
print("key = \(uid) and tam = \(guestN)")
}
}
controller.guestUser = selectedGuest
self.present(controller, animated: true, completion: nil)
}
我不知道selectedUser
与获取的数据有什么关系,但基本上代码的顺序是错误的。
由于observeSingleEvent
异步工作,您必须在完成闭包内实例化并呈现 controller。 我想如果selectedUser
匹配其中一条记录,您必须检查循环
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedGuest = guestsList[indexPath.row]
refGuests = Database.database().reference().child("userInfo").child(uid!).child("guests")
refGuests.observeSingleEvent(of: .value) { (snapshot) in
for snap in snapshot.children.allObjects as![DataSnapshot] {
let uid = snap.key
let guestD = snap.value as! [String:AnyObject]
let guestN = guestD["guestPhoneNumber"] as! String
print("key = \(uid) and tam = \(guestN)")
}
let controller = self.storyboard?.instantiateViewController(identifier: "GuestDetail") as! GuestDetailsViewController
controller.guestUser = selectedGuest
self.present(controller, animated: true, completion: nil)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.