I'm having trouble implementing a working like button in a table cell, using Parse as the backend. There is a button in the tablecell, which is called using a sender/tag. Here's the code.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"FeedCell";
FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
PFObject *post = [postArray objectAtIndex:indexPath.row];
cell.likeForYa.tag = indexPath.row;
[cell.likeForYa addTarget:self
action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
}
In the sender void, here's the code:
-(void)aMethod:(id)sender {
UIButton *senderButton = (UIButton *)sender;
NSLog(@"current Row=%d",senderButton.tag);
PFObject *tempObject = [postArray objectAtIndex:senderButton.tag];
NSLog(@"%@", tempObject.objectId);
//add the object ID for the cell we are liking to the array of liked items in the user class in parse
[[PFUser currentUser]addUniqueObject:tempObject.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
PFObject* like = [PFObject objectWithClassName:@"Like"];
[like setObject:[PFUser currentUser][@"username"] forKey:@"username"];
[like setObject:tempObject.objectId forKey:@"photo"];
[like saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFQuery *query = [PFQuery queryWithClassName:@"Like"];
[query whereKey:@"photo" equalTo:tempObject.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
NSLog(@"Number: %lu", (unsigned long)objects.count);
//cell.lik.text = [NSString stringWithFormat:@"%lu",(unsigned long)objects.count];
}];
}];
}
When the button is clicked, nothing is stores and the log for objects.count returns 0. Any ideas?