I have created a custom TableViewCell and currently have a button placed in the cell. When the button is pressed, In the tableviewcell.swift file,
In your situation, I will add a tag to your button to identify in which row it is. Whenever I configure cell in the call-back cellForRowAtIndexPath, I update this tag value.
When a button clicked, the handler specifies always the button pressed. With the tag defined to that pressed button, you can know the button of which row is pressed.
@IBAction func buttonPressed(sender: AnyObject) {
//convert to UIButton
if let btn = sender as? UIButton {
let rowId = btn.tag
//do your works
}
}
If your tableview has more than 1 sections, you will have to setup the value of tags the right way.
The second solution which is better: get the position of the button in your tableView, then get indexpath of that position in your tableview:
let position = sender.convertPoint(CGPointZero, toView: self.tblMain)
let indexPath = self.tblMain.indexPathForRowAtPoint(position)