我尝试在启动应用程序时填充表格视图,但是该方法不起作用。
var items = [ItemViewController.Item]()
override func viewDidLoad() {
super.viewDidLoad()
Database.database().reference(withPath: "items").observe(.childAdded, with: { snapshot in
var newItems: [ItemViewController.Item] = []
for child in snapshot.children { // children of "items"
if let snapshot = child as? DataSnapshot,
let item = ItemViewController.Item(snapshot: snapshot) {
newItems.append(item)
}
}
self.items = newItems
print(self.items)
self.tableView.reloadData()
})
}
(我的结构的初始化器)
init?(snapshot: DataSnapshot) {
guard let value = snapshot.value as? [String: AnyObject], let name = value["name"] as? String, let surname = value["surname"] as? String, let age = value["age"] as? Int else { return nil }
self.name = name
self.key = snapshot.key
//print("snapshot.key = \(snapshot.key)")
self.surname = surname
self.age = age
}