-1

I'm searching for a way to use a button in a cell. I've created it in the prototype cell of my table view but i can't get to detect the cell it's in when I tap on it.

Can you help me please ?

Ed Liss
  • 546
  • 4
  • 16
Heidan34
  • 1
  • 2

3 Answers3

1

Suppose this IBAction is the event handler for when you tap on the button:

-   (IBAction) buttonTapped: (UIButton *) sender
    {
       //get parent cell 
       UITableViewCell *cell = [sender findSuperViewWithClass:[UITableViewCell class]];
       //do whatever you want...
    }

where you'll need this category:

@implementation UIView (SuperView)

- (UIView *)findSuperViewWithClass:(Class)superViewClass
{
    UIView *superView = self.superview;
    UIView *foundSuperView = nil;

    while (nil != superView && nil == foundSuperView) {
        if ([superView isKindOfClass:superViewClass]) {
            foundSuperView = superView;
            break;
        } else {
            superView = superView.superview;
        }
    }
    return foundSuperView;
}

@end
Daniel Conde Marin
  • 7,588
  • 4
  • 35
  • 44
  • 1
    This is incorrect. The cell is not the superview of the button. In iOS 7 there are two other views between the button and the cell (the contentView and a scroll view), in iOS 6 there is one. If you are going to use this approach, you need to look up the superview chain for an object whose class is a UITableViewCell or subclass thereof. – rdelmar Feb 27 '14 at 23:04
  • Too bad, would have been easy that way :p – Heidan34 Feb 27 '14 at 23:10
  • @rdelmar: True, I have updated my answer, please check – Daniel Conde Marin Feb 27 '14 at 23:10
  • @user3362642: Were you able to do it with the updated answer? – Daniel Conde Marin Feb 27 '14 at 23:16
  • I'm trying but i don't get where the last part is going xD (i'm a beginner in obj-c) – Heidan34 Feb 27 '14 at 23:21
  • @Daniel I think i got this, the implementation must go before my TableViewController implementation. It seems to work now, trying to retrieve the row number ! – Heidan34 Feb 27 '14 at 23:25
  • @Daniel XCode doesn't like the fact that the method is returning an UIView in an UITableViewCell :p – Heidan34 Feb 27 '14 at 23:31
  • @Daniel I made a few modifications, and now it works just fine, thanks a lot – Heidan34 Feb 27 '14 at 23:40
0

"The way I handle buttons inside custom cells:

  • I define an IBAction that I connect to a UIButton event inside the custom cell
  • I define a delegate and a delegate method for the cell to handle the button action
  • The IBAction calls that delegate method
  • When defining your cells in cellAtRow... I set the tableViewController to be the delegate. cell.delegate=self;
  • I do whatever action I would like to do inside that delegate method in the tableViewController

"

from JP Hribovsek's answer of Handle button click inside UITableViewCell

Community
  • 1
  • 1
Jake Lin
  • 11,146
  • 6
  • 29
  • 40
0
- (void)buttonOnCellTapped:(id)sender
{        
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.table];
    NSIndexPath *indexPath = [self.table indexPathForRowAtPoint:buttonPosition];
    if (indexPath)
    {
        NSLog(@"BUTTON TAPPED ON SECTION: %d, ROW: %d", indexPath.section, indexPath.row);
        UITableViewCell *cell = [self.table cellForRowAtIndexPath:indexPath];
    }
}
Tom Redman
  • 5,592
  • 4
  • 34
  • 42