关于如何将表格视图与 Firebase 上的值连接起来,我自己有一个问题,或者更确切地说是一个误解。
我的资料:
我有一个 Firebase,许多其他文件夹之一称为“Flights”。在我的应用程序视图中,我可以按照以下路径在此文件夹中添加一些信息:Flights > userId > autoId > 我感兴趣的值,包括日期和其他一些字符串
我的问题是:
我如何添加一个 tableView,当这个是由 viewDidLoad 创建时,我的文件夹 Flight > userId 中的每个 autoId 一个新的个性化单元格?
我的尝试:
我声明我的数组:
var datas: [Flight] = []
我调用 viewDidLoad() 这个函数:
func loadFlights() {
ref = Database.database().reference()
let userID = Auth.auth().currentUser?.uid
ref.child("flights").child(userID!).childByAutoId().queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
if let valueDictionary = snapshot.value as? [AnyHashable:String]
{
let date = valueDictionary["Date"]
let type = valueDictionary["aircraft-model"]
let registration = valueDictionary["aircraft-registration"]
let totalTime = valueDictionary["TOTAL-TIME"]
let depTime = valueDictionary["departure-time"]
let depPlace = valueDictionary["departure-place"]
let arrTime = valueDictionary["arrival-time"]
let arrPlace = valueDictionary["arrival-place"]
self.datas.append(Flight(from: date ?? "XX-XX-XX", type!, registration!, totalTime!, depTime!, depPlace ?? "NIL", arrTime!, arrPlace!))
self.tableView.reloadData()
}else{
// nothing need to happens
}
})
}
最后我有我的表管理器部分:
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return datas.count
//return newFlight.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// creer la cellule
let cell = tableView.dequeueReusableCell(withIdentifier: "lbCells") as! LogBookCell
cell.dateLabel.text = datas[indexPath.row].date
cell.typeLabel.text = datas[indexPath.row].type
cell.RegistrationLabel.text = datas[indexPath.row].regi
cell.totalTimeLabel.text = datas[indexPath.row].totalTime
cell.depTimeLabel.text = datas[indexPath.row].depTime
cell.depPlaceLabel.text = datas[indexPath.row].depPlace
cell.arrTimeLabel.text = datas[indexPath.row].arrTime
cell.arrPlaceLabel.text = datas[indexPath.row].arrPlace
return cell
}
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete{
datas.remove(at: indexPath.row)
tableView.reloadData()
}
}
而且我不明白为什么当我在我的应用程序上加载 tableView 页面时什么都没有出现......
感谢您的帮助 !
传单 74