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 ?
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:
"
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];
}
}