如何解决Swift 4:从表视图传递到视图控制器 - Firebase?
我花了几个小时研究相同的问题,但我找到的答案都没有帮助解决这个问题。需要来自 Firebase 数据库的数据从 tableview 传递到另一个视图控制器。 PD需要显示的数据不在单元格中,我需要从单击表单视图行的 ID 从 firebase 获取。 我已经和手写的照片来解释我需要什么。
这是我来自 VC 1 的代码:
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return guestsList.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell",for: indexPath) as! GuestTableViewCell
let guest: GuestModel
guest = guestsList[indexPath.row]
cell.guestNameLabel.text = guest.guestName
cell.attendingLabel.text = guest.attendingStatus
cell.menuLabel.text = guest.menuStatus
cell.ageLabel.text = guest.ageStatus
cell.tableLabel.text = guest.tableNo
return cell
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
let selectedGuest = guestsList[indexPath.row]
let controller = self.storyboard?.instantiateViewController(identifier: "GuestDetail") as! GuestDetailsViewController
controller.guestUser = selectedGuest
self.present(controller,animated: true,completion: nil)
}
func tableView(_ tableView: UITableView,commit editingStyle: UITableViewCell.EditingStyle,forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let guest = guestsList[indexPath.row]
self.guestsList.remove(at: indexPath.row)
self.guestsTableView.reloadData()
self.deleteGuest(id: guest.id!)
}
}
和 VC 2
var ref: DatabaseReference!
let uid = Auth.auth().currentUser?.uid
var guestDetail = [GuestModel]()
var guestUser: GuestModel?
var documenID: String?
override func viewDidLoad() {
super.viewDidLoad()
showGuestDetails()
}
func showGuestDetails(){
ref = Database.database().reference().child("userInfo").child(uid!).child("guests")
ref.queryOrderedByKey().observeSingleEvent(of: .value) { (snapshot) in
if snapshot.childrenCount>0{
self.guestDetail.removeAll()
for guests in snapshot.children.allObjects as![DataSnapshot]{
let guestObject = guests.value as? [String: AnyObject]
let name = guestObject?["guestName"]
let familyName = guestObject?["guestFamilyName"]
let phone = guestObject?["guestPhoneNumber"]
let email = guestObject?["guestemail"]
let guest = GuestModel(guestName: name as? String,guestFamilyName: familyName as! String,guestPhoneNumber: phone as? String,guestemail: email as? String)
self.phoneNoLabel.text = guest.guestPhoneNumber
self.emailLabel.text = guest.guestemail
}
}
}
self.nameLabel.text = guestUser!.guestName
}'''
解决方法
您可以通过两种方式获得想要的结果:
-
将 autoId 从 1st VC 传递到 2nd Detail VC,并在
showGuestDetails()
中将您的引用设为ref = Database.database().reference().child("userInfo").child(uid!).child("guests").child(passedAutoID)
-
保持相同的引用,但使用 if 条件来检查用户是否与要求的相同
for guests in snapshot.children.allObjects as![DataSnapshot]{ let guestObject = guests.value as? [String: AnyObject] guard let email = guestObject?["guestEmail"] as? String,email != guestUser?.email else { continue } let name = guestObject?["guestName"] let familyName = guestObject?["guestFamilyName"] let phone = guestObject?["guestPhoneNumber" let guest = GuestModel(guestName: name as? String,guestFamilyName: familyName as! String,guestPhoneNumber: phone as? String,guestEmail: email as? String) self.nameLabel.text = guest.guestName self.phoneNoLabel.text = guest.guestPhoneNumber self.emailLabel.text = guest.guestEmail }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。