dan martin dan martin - 3 months ago 26
iOS Question

Swift - UITableview: How to make a tap on one cell cause change a label in another cell?

I have a UITableView, and I'd like a tap in cell X to cause the label text in cell Y to change from "Waiting..." to "Done". I'm pretty baffled as to how I should go about this. I've tried to do this using

didSelectRowAtIndexPath
, but seemingly I can only modify properties of the cell at the row tapped on.

Thanks!

Answer

Its quite easy to get a cell you need to change, but you have to know which row of your table it is:

class MyCell : UITableViewCell {

    @IBOutlet weak var myLabel: UILabel!

}

class TableViewController: UITableViewController {

    ...

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

        let row = 7 //or what you set
        let cellWhereIsTheLabel = tableView.cellForRow(at: IndexPath(row: row, section: 0)) as! MyCell
        cellWhereIsTheLabel.myLabel.text = "Done"

    }

    ...

}