I have a custom tableview
. And it contains a button
. And the question is how to know which button was tapped? From which indexPath.row
. I tried to do it in didSelectRowAt but, I am not selecting any cell, I am just pressing button
. I need it in SWIFT
Asked
Active
Viewed 775 times
0

Jack
- 291
- 2
- 6
- 12
-
please look at the [question which already answered yours](https://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview), though many are in ObjC they actually can easily be converted to Swift. If you need example codes you may also look at my answer I've added to that question. – Ben Ong Jul 24 '17 at 10:24
2 Answers
1
Below code will give you Button tag and also know which button is tapped. Right this code in cellForRowAtIndexPath UITableView DataSource Delegate Method.
cell.btn.tag = indexPath.row
cell.btn.addTarget(self, action: #selector(btnTapped(_:)), for: .touchUpInside)
func btnTapped(_ sender : UIButton) {
print(sender.tag)
}

devang bhatt
- 460
- 2
- 15
0
UIButton * btn = /* your button */;
UITableViewCell * cellContainingBtn = (UITableViewCell*)[btn superview];
UITableView * tbl = (UITableView *)[cellContainingBtn superview];
NSIndexPath * cellIndexPath = [tbl indexPathForCell:cellContainingBtn];
NSInteger index = [cellIndexPath row];
Answered here: Get row index of custom cell in UITableview
As an easy alternative, you could assign the indexPath.row of cell to the button's accessibilityLabel or accessibilityHint etc.

Rishabh
- 465
- 5
- 14