简体   繁体   English

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

[英]Why are the same comments showing for different user posts? (iOS, Swift, Parse)

Working on a social iPhone app using Swift (with a Storyboard) and Parse where users can create posts and comment on posts similar to the Facebook iOS app and other social network apps. 使用Swift(带有Storyboard)和Parse在社交iPhone应用程序上工作,用户可以在其中创建帖子并对类似于Facebook iOS应用程序和其他社交网络应用程序的帖子发表评论。

The app has an initial, master Home Feed page (which displays user posts) and a detail Reply page (which is supposed to display the comments for a particular post that was selected but is showing the same replies for different posts). 该应用程序具有一个初始的主主页Feed页面 (显示用户帖子)和一个详细的Reply页面 (该页面 显示所选特定帖子的评论,但对不同帖子显示相同的回复)。 Both use the PFTableViewController class and each have their own PFTableViewCell implemented in separate swift files as the prototype cells. 两者都使用PFTableViewController类,并且各自在单独的swift文件中作为原型单元实现了自己的PFTableViewCell。

When a user taps on ANY post cell in the Home Feed page, it navigates to the Reply page but shows all existing comments (as well as every new comment) for the post. 当用户在“主页供稿”页面上点击“任何帖子”单元格时,它将导航到“回复”页面,但显示该帖子的所有现有评论(以及每个新评论)。 I am trying to have only the comments for a specific post show when the user selects a particular post from the Home Feed page. 当用户从“主页供稿”页面中选择特定帖子时,我试图仅显示特定帖子的评论。

Any idea why this is happening? 知道为什么会这样吗? I greatly appreciate your time and help! 非常感谢您的宝贵时间和帮助!

Home Feed page: 主页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
        }


      }
   }
}

Reply page: 回复页面:

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

In your segue you need to tell the destination ViewController which post to show replies for. 在您的查询中,您需要告诉目标ViewController要显示回复的帖子。

Add this to the bottom of your segue (exactly where your comment is): 将其添加到segue的底部(正是您的评论所在的位置):

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

And in ReplyTableViewController you need a postObject variable so that the code in the segue works. 并且在ReplyTableViewController您需要一个postObject变量,以便segue中的代码起作用。 At the top of your ReplyTableViewController put: ReplyTableViewController的顶部放置:

var postObject = PFObject()

It looks like the postObject should be used somewhere in your PFQuery() to filter the replies, but I am not familiar with it. 它看起来像postObject应该在什么地方使用PFQuery()过滤的答复,但我不熟悉它。

I found a solution to my own problem! 我找到了解决自己问题的方法!

I have updated the Reply page to use UITableViewController instead of PFTableViewController and updated the storyboard correspondingly (I made the necessary changes in the code and in the Storyboard to comply with the constraints of UITableViewController, etc). 我已经更新了Reply页面,以使用UITableViewController而不是PFTableViewController并相应地更新了情节提要(我对代码和情节提要进行了必要的更改,以遵守UITableViewController等的约束)。

I implemented a PFQuery with the appropriate constraints to fetch all the replies for a given post (only) by writing something similar to the following: 我通过编写类似于以下内容的方式实现了具有适当约束的PFQuery,以(仅)获取给定帖子的所有答复:

query.whereKey("parent", equalTo: aPost)

// Finds objects *asynchronously* and call the given block with the results.
query.findObjectsInBackgroundWithBlock {
  (objects: [AnyObject]?, error: NSError?) -> Void in

   // if there is no error, for each object in `objects`,
   // assign the given object to a PFObject
   // add the object to an array that will store all of the applicable replies for the post
   // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM