【问题标题】:Firebase Retrieving and inserting in tableviewFirebase在tableview中检索和插入
【发布时间】:2017-05-06 18:38:10
【问题描述】:

我需要帮助检索孩子并将其添加到表格视图中。孩子被称为“Title”,是“Post”的子类。我已成功检索到所有帖子并打印出来,但我不知道如何将“标题”放在我的 tableview 中。

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

}

【问题讨论】:

    标签: swift uitableview firebase swift3 firebase-realtime-database


    【解决方案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()
    })
    

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2017-07-28
      • 2017-01-14
      • 1970-01-01
      • 2011-11-02
      相关资源
      最近更新 更多