在我正在开发的应用程序中,它允许用户在主时间线上发帖,而其他用户可以对该用户的帖子发表评论,所以我一直试图在 tableView 中显示评论,但它没有显示。我已经确认数据正在发布以进行解析,因此它按预期工作,但在显示评论时,我似乎无法让它工作。我正在使用此功能来显示评论:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("commentCell", forIndexPath: indexPath) as! CommentTableViewCell
cell.commentLabel?.text = comments![indexPath.row]
return cell
}
我的代码有什么问题吗?还是有其他方式显示评论?
最佳答案
检索评论的代码在哪里?确保在 for 循环之后调用“self.tableView.reloadData()”。
我通常从解析中检索信息的方式是这样的:
func query() {
var query = PFQuery(className: "comments")
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (caption2: [AnyObject]?, erreur: NSError?) -> Void in
if erreur == nil {
// good
for caption in caption2! {
self.comments.append(caption["<YOUR COLUMN NAME WHERE COMMENT IS STORED IN PARSE HERE>"] as! String)
}
self.tableView.reloadData()
}
else {
// not good
}
}
}
将这个函数添加到你的类中。然后改变这个:
func reply() {
post?.addObject(commentView!.text, forKey: "comments")
post?.saveInBackground()
if let tmpText = commentView?.text {
comments?.append(tmpText)
}
commentView?.text = ""
println(comments?.count)
self.commentView?.resignFirstResponder()
self.commentTableView.reloadData()
}
为此:
func reply() {
post?.addObject(commentView!.text, forKey: "comments")
post?.saveInBackground()
if let tmpText = commentView?.text {
comments?.append(tmpText)
}
commentView?.text = ""
println(comments?.count)
self.commentView?.resignFirstResponder()
self.query
}
关于ios - tableView 中不显示评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32428873/