为什么不同的用户帖子显示相同的评论? (iOS,Swift,Parse)

时间:2015-11-16 18:06:15

标签: ios iphone swift parse-platform pfquerytableviewcontrolle

使用Swift(带有故事板)和Parse在社交iPhone应用程序上工作,用户可以创建帖子并评论类似于Facebook iOS应用程序和其他社交网络应用程序的帖子。

该应用有一个初始的主主页Feed (显示用户帖子)和详细回复页应该显示已选择但对不同帖子显示相同回复的特定帖子的评论。两者都使用PFTableViewController类,并且每个都有自己的PFTableViewCell在单独的swift文件中实现作为原型单元格。

当用户点击“主页Feed”页面中的任何帖子单元格时,它会导航到“回复”页面,但会显示该帖子的所有现有评论(以及每个新评论)。当用户从“主页Feed”页面选择特定帖子时,我只想显示特定帖子的评论。

知道为什么会这样吗?非常感谢您的时间和帮助!

首页Feed页面:

class HomeTableVC: PFQueryTableViewController,CLLocationManagerDelegate {

  var posts: NSMutableArray! = NSMutableArray()

  override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {
    return posts.count
  }

  override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
  }

  override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    self.performSegueWithIdentifier("showReplyViewController", sender: self)

  }

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?, object: PFObject!) -> PFTableViewCell? {

    let cell = tableView!.dequeueReusableCellWithIdentifier("PostCell", forIndexPath: indexPath!) as! PostTableCell


    if let userPost : PFObject = self.posts.objectAtIndex(indexPath!.row) as! PFObject {

        cell.name.text = object["userName"] as? String
        cell.message.text = object["postMessage"] as? String
        let dateUpdated = object.createdAt! as NSDate
        let dateFormat = NSDateFormatter()
        dateFormat.dateFormat = "h:mm a"

        cell.dateTime.text =  NSString(format: "%@", dateFormat.stringFromDate(dateUpdated)) as String
        cell.message.numberOfLines = 0

        cell.message.text = userPost.objectForKey("postMessage") as? String
    }        

    return cell
}

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if (segue.identifier == "showReplyViewController") {

        let indexPath = self.tableView.indexPathForSelectedRow

        let postObject = self.objects![indexPath!.row] as! PFObject

        //postObject (on LHS) is the PFObject declared in ResponseViewController
        if let destinationVC = segue.destinationViewController as? ReplyTableViewController {

            destinationVC.postObject = postObject
        }


      }
   }
}

回复页面:

class ReplyTableViewController: PFQueryTableViewController {

var postObject: PFObject?

var replies: NSMutableArray! = NSMutableArray()

override func viewDidAppear(animated: Bool) {

    super.viewDidAppear(animated)

    replies = NSMutableArray()

    var replyQuery = PFQuery(className: "Reply")

    replyQuery.addAscendingOrder("createdAt")

    replyQuery.findObjectsInBackgroundWithBlock {
    (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            for object in objects! {
                let reply: PFObject = object as! PFObject
                self.replies.addObject(reply)
            }

            let repliesArray: NSArray = self.replies.reverseObjectEnumerator().allObjects

            self.replies = NSMutableArray(array: repliesArray)

            self.tableView.reloadData()

        }
    }

}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView?, numberOfRowsInSection section: Int) -> Int {

    return replies.count
}

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?, object: PFObject!) -> PFTableViewCell? {

  let cell = tableView!.dequeueReusableCellWithIdentifier("replyCell", forIndexPath: indexPath!) as! ReplyTableViewCell

  let replyObject: PFObject = self.replies.objectAtIndex(indexPath!.row) as! PFObject

  cell.replyMessageLabel.text = replyObject.objectForKey("replyMessage") as? String

  var queryUser: PFQuery = PFUser.query()!
    queryUser.whereKey("objectId", equalTo: (replyObject.objectForKey("replyUser")?.objectId)!)

  queryUser.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            let user: PFUser = (objects! as NSArray).lastObject as! PFUser
            cell.replyAuthorLabel.text = user.username
        }
  }

  return cell
}
}

1 个答案:

答案 0 :(得分:0)

在您的segue中,您需要告诉目标ViewController哪个帖子显示回复。

将其添加到您的segue底部(确切位置为您的评论):

if let destinationVC = segue.destinationViewController as? ReplyTableViewController{
  destinationVC.postObject = postObject
}

ReplyTableViewController中,您需要一个postObject变量,以便segue中的代码可以正常工作。位于ReplyTableViewController放置的顶部:

var postObject = PFObject()

看起来postObject应该在PFQuery()的某处使用来过滤回复,但我不熟悉它。