Search code examples
swiftparse-platformuibuttonsavepfquerytableviewcontrolle

Any idea why this func is writing/updating the first cell in table and not the selected one in PFQueryTableViewController?


In my app users can post to Parse backend and their posts are displayed in a timeline (PFQueryTableViewController). Each cell in this tableview has a button which the users can press to 'like' the post. Until yesterday I had the function to like sorted but suddenly today it is no longer working.

Now when a user likes a post it does a number of things: adds the user's objectId to an array in the post PFObject("likedBy"), and adds the post PFObject to a relation in the User class ("likedPosts").

But what seems to now be happening is it works but with the following problems:

  • it most often will like whatever post/cell is at the top of the tableview instead of the one the user actually clicks like on -- e.g. tap like button on the 4th post in the table and it will like the 1st post instead
  • it might like the intended post but then the user is unable to like anything else after that
  • or it may just not like anything at all

Here is the code I am using, where am I going wrong? It was working fine yesterday and I don't think it is a Parse backend problem because I set the app up to work with an entirely new backend an hour ago and the problem persists.

func likePost(sender: UIButton) {

        let hitPoint = sender.convertPoint(CGPointZero, toView:  self.tableView)
        let hitIndex = tableView.indexPathForRowAtPoint(hitPoint)
        let object = objectAtIndexPath(hitIndex)

        let userId = PFUser.currentUser()?.objectId

        let relayedResponses = (PFUser.currentUser()?.relationForKey("likedPosts"))! as PFRelation
        likedPosts.addObject(object!)
        PFUser.currentUser()?.saveInBackground()

        object?.addUniqueObject(userId!, forKey: "likedBy")
        object?.saveInBackground()

        self.tableView.reloadData()

}

And in my cellForRowAtIndexPath I have:

cell.likeButton.tag = indexPath.row
    cell.likeButton.addTarget(self, action: "likePost:", forControlEvents: UIControlEvents.TouchUpInside)

I will appreciate any help as this is DRIVING ME MAD!! I just can't see where the problem is!


Solution

  • You converting CGPointZero to tableView. Its is actually CGPoint(0,0) and always returns 0,0 which is first row of the table view.