Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call last cell in TableView

I want to change the image of the last cell if the user click on a new cell. For now im doing this:

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        //Old Cell Image
        let lastCell = tableView.cellForRow(at: self.lastCell) as! DataTableViewCell!;
        lastCell?.imageButton.image = UIImage(named: "RadioButtomDeactive.png")

        let indexPathCel = tableView.indexPathForSelectedRow;
        let currentCell = tableView.cellForRow(at: indexPathCel!) as! DataTableViewCell!;

        dateSelected = currentCell?.dateSession.text
        currentCell?.imageButton.image = UIImage(named: "RadioButtom.png")

    }

I tried to save the indexPath.row in lastCell (that is an int) but it didn't work since cellForRow only accept indexPath.

like image 395
Adrian Avatar asked Dec 07 '25 19:12

Adrian


2 Answers

You can simply create an IndexPath using your stored lastCell via:

IndexPath(row: self.lastCell, section: 0)

If there is more than one section, you'll just need to insert your last section instead of 0.

like image 151
dersvenhesse Avatar answered Dec 09 '25 16:12

dersvenhesse


Why don't you keep in memory the IndexPath instead of an Int ?

or

you can create an IndexPath from your Int with

IndexPath(row: Int, section: Int)
like image 24
Anthony Avatar answered Dec 09 '25 14:12

Anthony