user1469734 user1469734 - 5 months ago 26
Swift Question

IndexPath Swift TableView clicked column

With the following code I get the indexPath of the clicked button in the tableview:

@IBAction func btnClick(_ sender: UIButton) {
let hitPoint = sender.convert(CGPoint.zero, to: self.tableView)
if let indexPath = self.tableView.indexPathForRow(at: hitPoint){
print(indexPath)
print(indexPath.row)
}
}


The first print is
[3, 0]
when I clicked on the 3th row I get the
0
, but I need the other value. The
3
in this case.

Answer Source

indexPath returns [section, row].

If you want to access section (the thing you referred to as column ) , use indexPath.section