3

I have a custom UITableViewCell on which I have added a button, I have associated that button on an IBAction in my viewController. Now the problem that i am facing is how do I know from which cell that button was created. When I present my viewController which has a table in it and has multiple rows (custom UITableViewCell), now when the user presses the button the action is getting called, but how do I know which row was it.

Because based on the row index I need to store some value.

Edit: I have some clue on it now, but still I am not sure how will I do it, so it seems like on my tableViewController cellForRowAtIndexPath method I have to do something like this

[cell.button1 addTarget:self action:@selector(addToCart:) forControlEvents:UIControlEventTouchUpInside];

And then I have to write a method

-(IBAction) addToCart:(id) sender

But still what I don't know is how do i get the row index in my addToCart method. Appreciate your help.

Yogesh
  • 1,333
  • 3
  • 16
  • 35
  • See [this question](http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) or [this question](http://stackoverflow.com/questions/2846084/how-can-i-locate-the-indexpath-of-the-uitableviewcell-a-certain-button-is-in-when). –  Nov 05 '10 at 13:48
  • I tried as suggested on the above link UIButton *button = (UIButton *)sender; UITableViewCell *cell = (UITableViewCell*)[button superview]; int row = [[self tableView] indexPathForCell:cell].row; NSLog(@"The row id is %@", row); but this always returns row as 0. Could the problem be be that I am using a custom UITableViewCell ? – Yogesh Nov 08 '10 at 01:39
  • `row` is an int so use %d in the format string (not %@). –  Nov 08 '10 at 16:04
  • Also, it should be `UITableViewCell *cell = (UITableViewCell *)[button superview];` (need asterisk). If still doesn't work, you should post how you're adding the button to the cell in cellForRowAtIndexPath and the full button handler method. –  Nov 08 '10 at 16:19
  • Hi aBitObvious, Thanks for all your help, I have got it working probably the same way as you suggested. Thanks a lot. – Yogesh Nov 08 '10 at 16:24

4 Answers4

20

Ok, finally I got the answer, looking into different forums, people were suggesting to do something like this

in the custom table view controller in cellForRowAtIndexPath do this

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

where addToCart is name of UIButton in my customUITableViewCell. It didn't seems to work for me. So this is what I did

-(IBAction) addToCart:(id) sender{
        NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)
                    [[sender superview] superview]];
    NSLog(@"The row id is %d",  indexPath.row); 
 }

And then through interfacebuilder I associated the action of my button to addToCart IBAction on my table view controller.

Yogesh
  • 1,333
  • 3
  • 16
  • 35
  • You mean you associated the action of your button to addToCart IBAction on your custom UITableViewCell, right? – gotnull Jan 10 '11 at 05:47
  • 2
    With your example above row always returns 0 I did this instead: -(IBAction)addToCart:(id)sender { NSLog(@"The row id is %d", [sender tag]); } – MonkeyBlue Dec 15 '12 at 13:15
  • I'm trying this and it won't call the IBAction in my custom UITableViewCell. Is there anything more to your solution? – circuitlego Jul 18 '13 at 00:41
5

Much less hackerific.

[cell.button1 addTarget:self action:@selector(addToCart:event:) forControlEvents:UIControlEventTouchUpInside];


- (void)addToCart:(id)sender event:(id)event
{
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition];

}
iHorse
  • 491
  • 1
  • 5
  • 14
4

The accepted answer does not work any more. Please refer to this post

It does it that way:

- (void)checkButtonTapped:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath != nil)
    {
     ...
    }
}
Community
  • 1
  • 1
gsach
  • 5,715
  • 7
  • 27
  • 42
0

Many of the developer used custom view to show custom cell on Table view for older version of iOS. If you are one of them, then you will have to face a problem that your button click action will no longer work with iOS7.

How to resolve this:

You have two options:

Option 1: Create the new lay out with new table cell instead of taking view. and put all layouts again in table cell.

I know, this will require a lot of effort.If you don't want to do this, we have a very small hack for it:option 2

Option 2: Create a IBOutlet for your button and add this button as a subview of your cell's content view.

[self.myCell.contentView addSubview:self.btn_click];

The above line of code will add btn_click as a subview of you content view. Now button click action should work.

Shreesh Garg
  • 562
  • 5
  • 18