0

Can anyone tell me how to call a custom function created for a UIButton clicked inside a cell in a didSelectRowAtIndexPath. I have a shopping cart button in a tableViewCell, when clicked I am changing the icon and maintaining a shopping cart list which works fine. But what I am trying to achieve is when user clicks on any row this functionality of UIButton can be used as well.

I have written a following Code in cellForRowAtIndexPath

cell.addToCardButton.tag = indexPath.row;
[cell.addToCardButton addTarget:self action:@selector(CartTapped:) forControlEvents:UIControlEventTouchUpInside];

//cartTapped function defination
- (void)CartTapped:(UIButton*)sender {
    NSUInteger index = sender.tag;
    Product *product = [results objectAtIndex:index];
    NSIndexPath *ip = [NSIndexPath indexPathForRow:index inSection:0];

    if(sender.selected) {
        UIImage *btnImage = [UIImage imageNamed:@"AddCart.png"];
        [sender setImage:btnImage forState:UIControlStateNormal];
        [BasketObjects delete:product] ;

    } else {

        sender.imageView.image =[UIImage imageNamed:@"ClearCart.png"];
        [[BasketObjects sharedInstance].basketObjects addObject:product];
    }

    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationMiddle];
}
wonea
  • 4,783
  • 17
  • 86
  • 139
Adeel Ilyas
  • 81
  • 1
  • 9

1 Answers1

0

Do you want to execute the same action/method no matter where user will tap (cell or button)? Did I understand you correct? If yes just create method where you pass indexpath as parameter and don't create button - just UIImageView.

Piotr Wasilewicz
  • 1,751
  • 2
  • 15
  • 26