【发布时间】:2019-12-12 02:31:15
【问题描述】:
我的 viewController 中有两个 tableView,我试图从 firebase 获取信息。表 1 显示酒店列表,表 2 显示客人电子邮件列表。
我设法填充表 1,但表 2 只是空白。
var Hotels : [DataSnapshot] = []
var Guests : [DataSnapshot] = []
override func viewDidLoad() {
super.viewDidLoad()
tableView1.delegate = self
tableView1.dataSource = self
tableView2.delegate = self
tableView2.dataSource = self
if (tableView.tag == 1) {
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("Hotels").queryOrdered(byChild: "email").queryEqual(toValue: email).observe(.childAdded, with: { (snapshot) in
if let HotelDictionary = snapshot.value as? [String:AnyObject] {
if let typeOfhotel = HotelDictionary["typeOfHotel"] as? Double {
} else {
self.Hotels.append(snapshot)
self.tableView1.reloadData()
self.tableView2.reloadData()
print(snapshot)
}
}
})
}
}
else if (tableView.tag == 2)
{
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("Guest").queryOrdered(byChild: "Guest Email").queryEqual(toValue: email).observe(.childAdded, with: { (snapshot) in
if let guestDictionary = snapshot.value as? [String:AnyObject] {
if let typeOfemail = guestDictionary["Guest Email"] as? String {
} else {
self.Guest.append(snapshot)
self.tableView1.reloadData()
self.tableView2.reloadData()
print(snapshot)
}
}
})
}
}
我的 CellForRowAt 是
if (tableView.tag == 1) {
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("Hotels").queryOrdered(byChild: "email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in
let snapshot = self.hotelRequests[indexPath.row]
if let HotelDictionary = snapshot.value as? [String:AnyObject] {
if let typeOfhotel = HotelDictionary["typeOfHotel"] as? String {
}
})
}
}
else if (tableView.tag == 2) {
if let email = Auth.auth().currentUser?.email {
Database.database().reference().child("Guest").queryOrdered(byChild: "Guest Email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in
let snapshot = self.Guest[indexPath.row]
if let guestDictionary = snapshot.value as? [String:AnyObject] {
if let typeOfemail = guestDictionary["Guest Email"] as? String {
cell.textLabel?.text = typeOfemail
print(snapshot)
}
})
}
}
return cell
}
numberOfRows 的代码
if (tableView.tag == 1)
{
return self.Hotels.count
}
else if (tableView.tag == 2)
{
return self.Guest.count
}
else
{
return 0
}
}
我想查看两个表视图。带有酒店列表的 tableView1 和带有访客电子邮件的 tableView2
【问题讨论】:
-
您的代码@Nick 中有错字 - 检查我的更新答案
标签: swift firebase uitableview firebase-realtime-database