GOAT
GOAT

Reputation: 611

UIButton within Table View Cell issue

I'm having issues with my button that is located in side my UITableViewCell. I'm using storyboard and connected my button through IB. Within my cellforRowatIndexPath I added an action to my button:

cell.likeBtnPressed.tag = indexPath.row;
[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];

Below you will see what is called when the button is pressed:

-(void)userDidTapOnLikeButton:(UIButton *)button photo:(PFObject *)photo{
    //Disable the button so users cannot send duplicat requests
    [button setEnabled:NO];
    [button setTintColor:[UIColor redColor]];


    //Set the new state of the button
    BOOL liked = !button.selected;
    [button setEnabled:liked];

    //Get the current number of likes the post have
    NSString *originalButtonTitle = button.titleLabel.text;
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];

    //Update the like count in the ECDCache
    NSNumber *likeCount = [numberFormatter numberFromString:button.titleLabel.text];
    if (liked) {
        likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
        [[ECDCache sharedCache] incrementLikerCountForPhoto:photo];
    }else{
        if ([likeCount intValue] > 0) {
            likeCount = [NSNumber numberWithInt:[likeCount intValue] - 1];
        }

        [[ECDCache sharedCache] decrementLikerCountForPhoto:photo];
    }

    //Add the current user as a liker of the photo in ECDCache
    [[ECDCache sharedCache] setPhotoIsLikedByCurrentUser:photo liked:liked];

    //Update the button label
    [button setTitle:[numberFormatter stringFromNumber:likeCount] forState:UIControlStateNormal];

    //Call the appropriate static method to handle creating/deleting the right object
    if (liked) {
        [ECDUtility likePhotoInBackground:photo block:^(BOOL succeeded, NSError *error) {
            [button setEnabled:YES];
            [button setTitleEdgeInsets:UIEdgeInsetsMake(-1.0f, 0.0f, 0.0f, 0.0f)];
            [[button titleLabel] setShadowOffset:CGSizeMake(0.0f, -1.0f)];

            if (!succeeded) {
                // Revert the button title (the number) if the call fails
                [button setTitle:originalButtonTitle forState:UIControlStateNormal];
            }
        }];
    }
}

When ever I press the button I receive this:

-[UITouchesEvent objectId]: unrecognized selector sent to instance 0x15ee5de0

I'm not sure what I did wrong

Upvotes: 0

Views: 200

Answers (2)

Brian Nickel
Brian Nickel

Reputation: 27550

The problem is that the second parameter your method is receiving is not a PFObject, but a UIEvent.

There are three types of selectors you can send into addTarget:action:forControlEvents::

  • @selector(a): This method takes no parameters.
  • @selector(a:): This method has one parameter which is the UIControl which received the control event.
  • @selector(a:b:): This method has two parameters, the UIControl which received the control event, and the UIEvent that triggered it.

Since you are only looking to get the button, you should have a signature like this:

-(void)userDidTapOnLikeButton:(UIButton *)button
{
    PFObject *photo = [self someLogicToGetThePhotoFromTheButton:button];
    ...

Upvotes: 1

Andrés Brun
Andrés Brun

Reputation: 185

you can't add a target with more than one parameter in UIButton selector. When the selector is called receive only the sender parameter, in that case, the UIButton object. So I recommend that you use:

[cell.likeBtnPressed addTarget:self action:@selector(userDidTapOnLikeButton:photo:) forControlEvents:UIControlEventTouchUpInside];

-(void)userDidTapOnLikeButton:(id)sender{
  //Your code
}

And then retrieve the photo using the tag of the cell for example.

Good luck ;)

Upvotes: 0

Related Questions