问题
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?
回答1:
So here you subclass with PFQueryTableViewController and your tableViewCell may look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
static NSString *CellIdentifier = @"FeedCell";
FeedCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIButton *likeForYa = [UIButton buttonWithType:UIButtonTypeCustom];
[likeForYa setTag:CellLikeForYaTag];
[cell.contentView addSubview:likeForYa];
[likeForYa addTarget:self
action:@selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
}
UIButton * likeForYa = (UIButton*) [cell.contentView viewWithTag:CellLikeForYaTag];
// check if current user like the post and change like-button image accordingly
if ([[object objectForKey:@"whoLiked"]containsObject:[PFUser currentUser].objectId]) {
[likeForYa setImage:[UIImage imageNamed:@"pressedLike.png"] forState:UIControlStateNormal];
} else {
[likeForYa setImage:[UIImage imageNamed:@"unpressedLike.png"] forState:UIControlStateNormal];
}
}
And here's the aMethod:
- (void)aMethod:(UIButton *)button{
CGPoint hitPoint = [button convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];
PFObject *object = [self.objects objectAtIndex:hitIndex.row];
// check if current user already liked the post
if (![[object objectForKey:@"whoLiked"]containsObject:[PFUser currentUser].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:object.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
//add the user ID to the post that the user liked
[object addUniqueObject:[PFUser currentUser].objectId forKey:@"whoLiked"];
[object saveInBackground];
} else {
//remove the object ID for the cell we are liking to the array of liked items in the user class in parse
[[PFUser currentUser] removeObject:object.objectId forKey:@"liked"];
[[PFUser currentUser] saveInBackground];
//remove the user ID to the post that the user liked
[object removeObject:[PFUser currentUser].objectId forKey:@"whoLiked"];
[object saveInBackground];
}
[self.tableView reloadData];
}
回答2:
Alway get aware of result that is throwing error or not.
PFObject* like = [PFObject objectWithClassName:@"Like"];
[like setObject:[PFUser currentUser][@"username"] forKey:@"username"];
[like setObject:tempObject.objectId forKey:@"photo"];
[like saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
//1. Check isSuccess
if (succeeded) {
PFQuery *query = [PFQuery queryWithClassName:@"Like"];
[query whereKey:@"photo" equalTo:tempObject.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error == nil) {
NSLog(@"Number: %lu", (unsigned long)objects.count);
//cell.lik.text = [NSString stringWithFormat:@"%lu",(unsigned long)objects.count];
}
}];
}
}];
Another thing is make sure your DataTable/Class/Object Structure in the parse.com is matches the name and also type what you are passing here.
Just like
"Like" class that have field "username" and it's type String. And "photo" that have type same like (tempObject.objectId).
来源:https://stackoverflow.com/questions/32535484/implementing-a-like-button