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