【发布时间】:2017-03-31 00:58:59
【问题描述】:
这是我用来从 firebase 获取用户的 cmets 和用户名以显示在 cmets 的 tableview 单元格中的代码。它目前不显示任何内容,但是当我发布 cmets 时,它会通过并且我可以在 firebase 数据库中看到它。我只需要阅读所有评论数据并显示出来。
func fetchComment()
{
let ref = FIRDatabase.database().reference().child("Newsfeed").child(itemSelected.title)
ref.observeSingleEvent(of: .value, with: { snapshot in
if snapshot.hasChild("comments")
{
ref.child("comments").observeSingleEvent(of: .value, with: {snappy in
for comPosted in snappy.children.allObjects as! [FIRDataSnapshot]
{
let commentPosted = comment()
guard let comDict = comPosted.value as? [String: AnyObject]
else
{
continue
}
commentPosted.commentText = comDict["userComment"] as! String!
commentPosted.username = comDict["userId"] as! String!
self.comments.append(commentPosted)
}
})
}
})
self.commentTableView.reloadData()
ref.removeAllObservers()
}
【问题讨论】:
-
从 firebase 获取数据后,您在哪里尝试重新加载表视图?
-
哦,对不起,我在最后一行代码之前有 self.commentTableView.reloadData() 。但它仍然没有显示任何内容。
-
您必须在
for循环之后调用reloadData,包括完成块。现在,您甚至早在检索数据之前就调用了reloadData。 -
谢谢,但不幸的是它仍然没有显示它:(
-
您需要提供更多详细信息。展示您对
reloadData的更新使用以及相关的表格视图数据源方法。
标签: ios swift firebase firebase-realtime-database