How can I use a button in a cell (placed with StoryBoard)

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 ?

Answers


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

"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


- (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];
    }
}

Need Your Help

MS SQL Upsert to mySQL Equivalent

mysql sql-server

I'm moving a msSQL database to mySQL. In my MS SQL database I have stored procedures that use an "upsert" (update, if @@rowcount = 0, Insert) type of thing.

In Excel trying to update rows in Sheet 1 from Sheet 2 based on a unique id found in both sheets

excel excel-vba vba

My question is similar to some found on this forum, but I can't quite get it to work. I have a master sheet in excel that has all my data with many columns and rows. What happens is, I receive upda...