Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying the text in a static UITableView cell?

I'm using a "Static Cells" table view created in my storyboard file. However I'd like to update the text on one of these cells when a setting is changed.

I'm pushing a view controller which updates the setting. I'm trying to use a callback to change the cell's text when it's popped off the stack, but by that point the cell has apparently been recycled or reused, so the old object is off screen and no longer used in the table view.

Is there a way I can update this text, and make it permanent (so that when the cell is scrolled off screen and comes back, the new value still appears)?

like image 408
jtbandes Avatar asked Feb 18 '12 16:02

jtbandes


2 Answers

Assuming your table view hierarchy is along the lines of:

Table View (static cells)
 - Table View Section
  - Table View Cell
    - UILabel (with the text property you want to modify)
  1. Declare an IBOutlet UILabel in your code, and wire it up in the storyboard's UILabel in the table view hierarchy above.
  2. In your callback method, set your UILabel's text property as you see fit.
like image 100
Marco Avatar answered Sep 24 '22 07:09

Marco


You can store text that you want to change as a property and use it in:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath)

    switch (indexPath.section, indexPath.row) {
    case (0, 3):
        cell.textLabel?.text = yourProperty

    default: break
    }

    return cell
}
like image 27
rafalkitta Avatar answered Sep 24 '22 07:09

rafalkitta