关于如何将表视图与Firebase上的值相关联,我遇到了一个问题,或更确切地说是一个误解。
我的信息:
我有一个Firebase,其中许多其他文件夹之一称为“航班”。在我的应用程序视图中,我可以按照以下路径在此文件夹中添加一些信息:Flights> userId> autoId>我感兴趣的值,包括日期和其他String
我的问题是:
当viewDidLoad创建一个tableView时,如何通过我的文件夹Flights> userId中的每个autoId创建一个新的个性化单元格?
我的尝试:
我声明了我的数组:
Child
我在viewDidLoad()上调用此函数:
var datas: [Flight] = []
最后我有表管理器部分:
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
}
})
}
而且我不明白为什么在我的应用程序中加载tableView页面时什么也没出现...
感谢您的帮助!
Flyer-74
答案 0 :(得分:0)
您何时/何地设置表格视图?
func setUpTable() {
tableView.delegate = self
tableView.dataSource = self
registerCell(name: "yourcell")
}
请确保在致电loadFlights()
答案 1 :(得分:0)
您可以在添加或删除时观察值。
var ref: DatabaseReference! = Database.database().reference()
ref.child("devices").child(user!.uid).child("device_name").setValue("iPhone test")
let newRef = ref.child("rooms").childByAutoId()
newRef.child("name").setValue("Room test")
ref.child("devicesrooms").child(newRef.key).child(user!.uid).setValue(true)
let ev = ref.child("devicesrooms").child(newRef.key)
var deviceNames = [String: String]()
var deviceHandles = [String: UInt]()
// Listen for added devices
ev.observe(.childAdded, with: { (snapshot) -> Void in
// Retrieve device key
let deviceKey = snapshot.key
// Add observer on device
var handle: UInt = 0
handle = ref.child("devices").child(deviceKey).observe(.value, with: { (snapshot) in
let value = snapshot.value as? NSDictionary
let deviceName = value?["device_name"] as? String ?? ""
// Update or Add deviceName and handle in your dictionary
deviceNames[deviceKey] = deviceName
deviceHandles[deviceKey] = handle
})
})
// Listen for removed devices
ev.observe(.childRemoved, with: { (snapshot) -> Void in
let deviceKey = snapshot.key
// Remove deviceName from your dictionary
deviceNames[deviceKey] = nil
// Retrieve handle from your dictionary
if let handle = deviceHandles[deviceKey] {
// Remove observer on device
ref.child("devices").child(deviceKey).removeObserverWithHandle(handle)
}
// Remove handle from your dictionary
deviceHandles[deviceKey] = nil
})