0

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!

Chris Wright
  • 89
  • 2
  • 8

1 Answers1

0

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

Juri Noga
  • 4,363
  • 7
  • 38
  • 51
  • Thanks for that! Do you know how I would go about making certain that it only likes the post/row that I click and not the first row? – Chris Wright Dec 10 '15 at 17:02
  • Use button's `tag` like that `let index = sender.tag` and then `let object = objectAtIndexPath(index)` – Juri Noga Dec 10 '15 at 17:03
  • Ah this is making more sense now... But now I have an error in the objectAtIndexPath(index) line saying 'cannot convert value of type 'Int' to expected argument type 'NSIndexPath?' – Chris Wright Dec 10 '15 at 17:16
  • So change to that. `let index = NSIndexPath(forItem: sender.tag, inSection: 0)` – Juri Noga Dec 10 '15 at 17:18
  • Perfect! Oddly enough I tried the code I had before and it was working again, but I've now opted for your revisions as I know for definite they work and I now understand why they work. Thanks! – Chris Wright Dec 10 '15 at 17:33