dan martin
dan martin

Reputation: 1317

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!

Upvotes: 1

Views: 1530

Answers (2)

Mindsers
Mindsers

Reputation: 662

A solution will be to update the datasource of your tableview.

When user taps on cell, didSelectRowAtIndexPath is called. In this function you can update datasource to provoke changes in other cells and then call reloadData to apply changes.

When you call reloadData tableView is updated by cellForRowAtIndexPath. This function uses your datasource to update cell.

Upvotes: 1

pedrouan
pedrouan

Reputation: 12910

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"

    }

    ...

}

Upvotes: 3

Related Questions