Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift3 cast int to indexPath

Tags:

swift3

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!

like image 577
Anti_Blume Avatar asked Dec 24 '22 21:12

Anti_Blume


1 Answers

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.

like image 189
Kie Avatar answered Jun 16 '23 12:06

Kie