我编写了一个 firebase 查询,它从我的竞争数据库中获取信息,然后在填充 TableView 之前将其加载到我的 distanceData(distanceModel 类型,我创建的一个类)中。但是, TableView 未被填充。
Firebase 数据库照片
distanceModel类图片
我尝试在查询中放置打印语句以查看是否正确检索数据,但没有打印任何内容,因此问题很可能出在查询上。
var distanceData = [distanceModel]()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
self.tableView.rowHeight = 200
refUser = Database.database().reference().child("userInfo");
let userID = Auth.auth().currentUser!.uid
let query = refUser?.queryOrdered(byChild: "userId").queryEqual(toValue: "\(userID)")
query?.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let childSnap = child as! DataSnapshot
var dict = childSnap.value as! [String: Any]
let level=dict["level"] as! String
if (level=="Beginner") {
self.refCompetition = Database.database().reference().child("competition1");
}
else if (level=="Intermediate") {
self.refCompetition = Database.database().reference().child("competition2");
}
else {
self.refCompetition = Database.database().reference().child("competition3");
}
}
})
检索帖子并监听变化
refCompetition?.observe(DataEventType.value, with: { (snapshot) in
for users in snapshot.children.allObjects as! [DataSnapshot] {
let userObject = users.value as? [String: AnyObject]
let userName = userObject?["name"] as! String?
let userDistance = userObject?["distance"] as! String?
print(userName)
print(userDistance)
let dist = distanceModel(name: userName,distance: userDistance)
self.distanceData.append(dist)
}
})
self.tableView.reloadData()
}
行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return distanceData.count;
}
每个单元格的内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DistanceCell", for: indexPath) as! DistanceCell
let dist: distanceModel
dist = distanceData[indexPath.row]
cell.nameLabel.text = dist.name
cell.distanceLabel.text = dist.distance
cell.rankNumber.text = String (indexPath.row+1)
return cell
}
它应该从适当的竞争数据库中获取数据,然后将结果呈现到 TableView 中。
最佳答案
那个呢?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
self.tableView.rowHeight = 200
refUser = Database.database().reference().child("userInfo");
let userID = Auth.auth().currentUser!.uid
let query = refUser?.queryOrdered(byChild: "userId").queryEqual(toValue: "\(userID)")
query?.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let childSnap = child as! DataSnapshot
var dict = childSnap.value as! [String: Any]
let level = dict["level"] as! String
if (level == "Beginner") {
self.refCompetition = Database.database().reference().child("competition1");
}
else if (level == "Intermediate") {
self.refCompetition = Database.database().reference().child("competition2");
}
else {
self.refCompetition = Database.database().reference().child("competition3");
}
self.refCompetition?.observe(.value, with: { (snapshot) in
for users in snapshot.children.allObjects as! [DataSnapshot] {
let userObject = users.value as? [String: AnyObject]
let userName = userObject?["name"] as! String?
let userDistance = userObject?["distance"] as! String?
print(userName)
print(userDistance)
let dist = distanceModel(name: userName,distance: userDistance)
self.distanceData.append(dist)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
})
}
})
}
关于ios - Firebase 查询没有为 TableView 填充数组。我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55819976/