Romowski Romowski - 5 months ago 39
iOS Question

How to get UITableView's row and section by UIButton tag?

There is

UITableViewCell
which contain
UIButton
.

In my cellForRowAtIndexPath:

cell.toCartBtn.tag = indexPath.row;
[cell.toCartBtn addTarget:self
action:@selector(toCart:) forControlEvents:UIControlEventTouchDown];


In toCart method I need to get row and section by tapped button tag. How can I do it?

Answer

See this code

- (void)toCart:(id)sender
{
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
    if (indexPath)
    {
     ...
    }
}
Comments