Swift Firebase在tableview中检索和插入
我需要帮助检索一个子项并将其添加到tableview中。子类称为Title,是Post的子类。我已经成功地检索并打印了所有文章,但我不知道如何将标题放在我的tableview中Swift Firebase在tableview中检索和插入,swift,uitableview,firebase,swift3,firebase-realtime-database,Swift,Uitableview,Firebase,Swift3,Firebase Realtime Database,我需要帮助检索一个子项并将其添加到tableview中。子类称为Title,是Post的子类。我已经成功地检索并打印了所有文章,但我不知道如何将标题放在我的tableview中 var posts = [postStruct]() override func viewDidLoad() { super.viewDidLoad() let ref = FIRDatabase.database().reference().child("Posts") ref.obs
var posts = [postStruct]()
override func viewDidLoad() {
super.viewDidLoad()
let ref = FIRDatabase.database().reference().child("Posts")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
print(rest.value!)
//need help here! I want to put the title which is a child of "Post"
}
})
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let label1 = cell?.viewWithTag(1) as! UILabel
label1.text = posts[indexPath.row].title
return cell!
}
}
let ref = FIRDatabase.database().reference().child("Posts")
ref.observeSingleEvent(of: .value, with: { snapshot in
print(snapshot.childrenCount)
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let value = rest.value as? Dictionary<String,Any> else { continue }
guard let title = value["Title"] as? String else { continue }
let post = postStruct(title: title)
self.posts.append(post)
}
self.tableView.reloadData()
})