How to get the indexPath.row of a particular cell when a button in the cell is tapped here is my code
if let indexPath = self.tableView?.indexPathForSelectedRow {
print("\(indexPath.row)")
}
How to get the indexPath.row of a particular cell when a button in the cell is tapped here is my code
if let indexPath = self.tableView?.indexPathForSelectedRow {
print("\(indexPath.row)")
}
Get the cell from button(sender) action
var cell: UITableViewCell = sender.superview.superview as UITableViewCell
Get the indexpath
tableView.indexPathForCell(cell)
When a button is tapped in your cell, you need to traverse up the chain of that button's superviews to obtain reference to the UITableViewCell. Once there, you can ask the tableView for the indexPath by using the tableView.indexPathForCell(cell: UITableViewCell)
function.
Something like this:
var optionalCell: UITableViewCell? = nil
var view: UIView = yourButton
while view.superView != nil {
view = view.superView
if view is UITableViewCell {
optionalCell = view
break
}
}
if let cell = optionalCell {
var indexPath = tableView.indexPathOfCell(cell)
} else {
return
}
I don't know how your code looks, but that is what it should look like. Of course, you could always hack it I suppose and know exactly how many superViews up the cell (UITableViewCell) is compared to your button, but this'll work regardless of how deep within your cell view hierarchy the button is.