实现一个喜欢的按钮

Implementing a like button

我在使用 Parse 作为后端时在 table 单元格中实现类似按钮的工作时遇到问题。 table 单元格中有一个按钮,使用 sender/tag 调用。这是代码。

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


}

在发件人空白处,代码如下:

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




 }

单击按钮时,不会存储任何内容,objects.count returns 0 的日志。有什么想法吗?

始终了解是否抛出错误的结果。

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

            }];

    }

}];

另一件事是确保 parse.com 中的 DataTable/Class/Object 结构与名称匹配,并在此处输入您传递的内容。

喜欢

"Like" class 具有字段 "username" 并且它是字符串类型。 "photo" 的类型与 (tempObject.objectId).

相同

所以在这里你使用 PFQueryTableViewController 进行子类化,你的 tableViewCell 可能看起来像这样:

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

  }

}

这是 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];

}