2017-05-06 56 views
0

我需要幫助檢索一個孩子並將其添加到tableview中。這個孩子被稱爲「標題」,是「發佈」的子類。我已經成功地檢索了所有的帖子並將其打印出來,但我不知道如何在桌面視圖中放置「標題」。Firebase在桌面視圖中檢索和插入

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! 
} 

}

回答

1
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() 
}) 
+0

它的工作!非常感謝你特里斯坦! – Riccardo