【问题标题】:Comments not Displayed in tableViewtableView 中不显示评论
【发布时间】:2015-09-06 22:24:32
【问题描述】:

在我正在开发的应用程序中,它允许用户在主时间轴上发帖,其他用户可以评论该用户的帖子,所以我一直在尝试在 tableView 中显示 cmets,但它没有显示。我已经确认数据正在发布以进行解析,因此它按预期工作,但是在显示 cmets 时,我似乎无法让它工作。我正在使用这个函数来显示 cmets:

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
}

我的代码有什么问题吗?还是有其他显示 cmets 的方法?

【问题讨论】:

    标签: ios iphone swift cocoa-touch parse-platform


    【解决方案1】:

    检索 cmets 的代码在哪里?确保在 for 循环之后调用“self.tableView.reloadData()”。

    我通常从 parse 中检索信息的方式是这样的:

        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
     }
    

    【讨论】:

    • 我刚刚添加了我的整个班级
    • 好的,我添加了该函数并修改了reply() 函数,但我在self.comments.append(caption["comments"] as! String) 中遇到错误,说无法使用列表类型字符串的参数调用追加和错误此处:self.tableView.reloadData() 无法在没有参数的情况下调用 reloadData
    • 我可以修复reloadData 错误,而不是使用tableView.reloadData() 我可以使用commentTableView.reloadData()
    • 在顶部,而不是 var cmets: [String]?尝试 var cmets = [String]()
    • 我读到错误:cannot invoke append with an argument of list type string 发生,因为通常在尝试附加到可选数组时调用
    【解决方案2】:

    原来我失踪了:

    UITableViewDataSource 
    

    在我的课堂上,所以修复了它:

    class DetailViewContoller: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextViewDelegate {
    
    ...
    ...
    ...
    
    override func viewDidLoad() {
    super.viewDidLoad()
    commentTableView.delegate = self
    commentTableView.dataSource = self
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-05
      • 2012-10-14
      • 2015-10-11
      • 2018-10-12
      • 2017-01-03
      • 1970-01-01
      相关资源
      最近更新 更多