1

I have a UIButton in a custom UITableViewCell. This button triggers an event when clicked.

[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside]; 

The method which is called when the button is clicked is :

 - (void) buttonClicked:(id)sender
{

UIButton *b = (UIButton*)sender;

.....

} 

My question is, how can I get an instance of the cell in which the button is placed?

Teo
  • 3,394
  • 11
  • 43
  • 73
  • 1
    possible duplicate of [How to know the UITableview row number](http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number) - see my answer there for a good solution to this. – jrturton Nov 22 '12 at 09:42

5 Answers5

2
 UITableViewCell *cell = (UITableViewCell*)[[b superview] superview];

If you added the button on cell as subview then superview of button will be contentView and superview of contentView will UITableViewCell

indu
  • 323
  • 2
  • 18
  • 1
    Rather than only post a block of code, please *explain* why this code solves the problem posed. Without an explanation, this is not an answer. – Martijn Pieters Nov 22 '12 at 09:39
  • If you added the button on cell as subview then superview of button will be contentView and superview of contentView will UITableViewCell. – indu Nov 22 '12 at 09:49
  • DON'T USE THIS SOLUTION. This will result in broken code under newer iOS versions. Don't depend on superviews you don't own not changing. – KDM Jun 20 '13 at 22:58
0
- (void) buttonClicked:(id)sender
{


  UIButton *b = (UIButton*)sender;    
  UITableViewCell *tableViewCell =(UITableViewCell*) [b superview];  

 //if you added the UIButton as a subview of UITableViewCell contendView, use this
  UITableViewCell *tableViewCell =(UITableViewCell*) [[b superview] superview]; 

} 
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0

You can set tag to button when you are adding the buttons in table view cell. It will be better if you use same indexPath of able view cell. Then from [sender tag] you able to get the indexPath value.

Susim Samanta
  • 1,605
  • 1
  • 14
  • 30
0

I'd suggest you not just to put a button into a UITableViewCell. What you should better to is to subclass UITableViewCell and make UITableViewButtonCell that will cal a delegate callback like this: - (void) tableViewButtonCellDidButtonTap:(UITableViewCell*) buttonCell This will be more SOLID solution.

The case with UIButton *b = (UIButton*)sender;
UITableViewCell *tableViewCell =(UITableViewCell*) [b superview];
will only work if your button lies DIRECTLY insade the UITableviewCell that can possibly be not a case.

eagle.dan.1349
  • 611
  • 1
  • 8
  • 20
0

As you rightly say, the button is "in" the cell - in the sense that it is a subview, at some depth, of some cell. So simply walk up the view hierarchy until you reach the cell:

- (void) buttonClicked:(id)sender {
    UIView* v = sender;
    while (![v isKindOfClass:[UITableViewCell class]])
        v = v.superview;
    // now v is the cell
}
matt
  • 515,959
  • 87
  • 875
  • 1,141