0

Having a problem implementing Like/Unlike function in my UITableView. I am using Parse.com database, so far the like and unlike function actually works, but the UITableView are showing wrong information if i scroll down a bit.

Using NSLog gets me the correct row i actually like. I am querying the like class that i have created in the database to compare against the object at the indexPath, if they matches the unlike button should be visible. Problem is that if i like the first row (index 0) the unlike button will also be shown on index 5,10,15 etc. I Got 3,5 cells visible at the screen from the start. Here's my code that is in cellForRowAtIndexPath

PFQuery *getLike = [PFQuery queryWithClassName:@"Likes"];
    [getLike whereKey:@"user" equalTo:[PFUser currentUser]];
    [getLike whereKey:@"post" equalTo:[mainFeed objectAtIndex:indexPath.row]];

    [getLike findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error){
        if(!error){
            PFUser* userIClickedOnInTheTableview = [PFUser currentUser];
            PFObject *checkObj = [mainFeed objectAtIndex:indexPath.row];

            for (PFObject *object in objects) {
                PFUser* checkUser = [object objectForKey:@"user"];
                PFObject *likeObj = [object objectForKey:@"post"];
                if([userIClickedOnInTheTableview.objectId isEqualToString:checkUser.objectId] && [checkObj.objectId isEqualToString:likeObj.objectId]){
                    NSLog(@"You like this row: %d", indexPath.row);

                    MIHFeedCell *thecell = (MIHFeedCell*)[tableView cellForRowAtIndexPath:indexPath];
                    thecell.likeBtn.hidden = YES;
                    thecell.unlikeBtn.hidden = NO;

                }else{
                    MIHFeedCell *thecell = (MIHFeedCell*)[tableView cellForRowAtIndexPath:indexPath];
                    thecell.likeBtn.hidden = NO;
                    thecell.unlikeBtn.hidden = YES
                    ;

                }
            }}
    }];
Code cracker
  • 3,105
  • 6
  • 37
  • 67
Mikzstone
  • 1
  • 3
  • there was a solution for [like button](https://stackoverflow.com/questions/32535484/implementing-a-like-button) unlike transfer to perse remove – Erinson Apr 16 '18 at 14:40

1 Answers1

0

That code should not be present in cellForRowAtIndexPath which gets run every time the contents of the tableView changes (all the time when scrolling).

Instead, run that code at the start and less often, like when the view appears and when the user requests an update. The completion block should put the returned objects in an NSArray property and reloadData on the table.

The table delegate methods should just refer to the current state of those objects (not try to change their state). The cellForRowAtIndexPath should be careful to set the cell state for both sides of the "is it liked" condition (I think your code does that), so reused cells have correct state in all cases.

danh
  • 62,181
  • 10
  • 95
  • 136
  • i dont think i really understand what you mean, i did put the download method in viewDidLoad instead just to try, but i do the comparison in the cellForRowAtIndexPath, because i need to check with the database if the object on the row exists in there. – Mikzstone Nov 21 '13 at 19:03
  • You can compare in the `viewDidLoad` itself. Fetch the values, compare them and show in `cellForRowAtIndexPath` – Hemant Singh Rathore Apr 01 '15 at 10:00