is there any chance to cast an Integer to the indexPath ?
Because I need the text out from the cell when I press the button. Or can I use a String as a sender for the Button?
cell.uncheckbox.tag = indexPath.row as! IndexPath
cell.uncheckbox.addTarget(self, action: #selector(ViewController.btnClicked), for: UIControlEvents.touchUpInside)
func btnClicked(sender: UIButton)
{
let row = sender.tag
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: row) as! TableViewCell;
print("cellentext \(cell.textLabel?.text)")
}
Thanks for your help!
What you are currently do is get a new cell for the row in which the button is. Which is probably not what you need.
let row = sender.tag
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: row) as! TableViewCell;
What you want is use the tag
as the row parameter in your IndexPath
. You can construct your own IndexPath like this, supposing you only have one section:
let indexPath = IndexPath(row: tag, section: 0)
And then use it to get your current cell by using the cellForRow(at: IndexPath)
method of your UITableView
.
let cell = tableView.cellForRow(at: indexPath)
Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With