Search code examples
iosios5uitableviewuibutton

Get a button inside tableview cell to run the same as didSelectCell


I have a tableview with a UIButton within the cell.

I have created some functionality within didSelectRowAtIndexPath. What I would like is for the same functionality/method to run when the user clicks on the button too.

How can I make the buttonPressed method run the didSelectRowAtIndexPath?

If I cannot do this, I can move the functionality to a new method and have both call this new method. However, how do I get the cell indexPath from the button pressed method?


Solution

  • -(IBAction)playButtonPressed:(id)sender {
        NSLog(@"button pressed");
        UIButton *button = (UIButton *)sender;
        UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
        UITableView *curTableView = (UITableView *)cell.superview;
        NSIndexPath *indexPath = [curTableView indexPathForCell:cell];
    
        [self didSelectRowOrButtonAtIndexPath:indexPath];
    }
    

    I made a new method didSelectRowOrButtonAtIndexPath and called this from both the buttonPressed and the didSelectRowAtIndexPath and pass in the indexpath to be used in my method.