实现类似按钮

时间:2015-09-12 05:27:53

标签: ios objective-c uitableview parse-platform pfobject

我在使用Parse作为后端时在表格单元格中实现类似按钮的按钮时遇到了麻烦。 tablecell中有一个按钮,使用发送者/标签调用。这是代码。

- (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的日志返回0.任何想法?

2 个答案:

答案 0 :(得分:1)

所以在这里你使用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];

}

答案 1 :(得分: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 Structure与名称匹配,并键入您在此处传递的内容。

就像

"像"具有字段"用户名"的类它的类型为String。并且"照片"类型相同的(tempObject.objectId)。