ppp
ppp

Reputation: 771

UITableViewCell - content reset when scrolling up or down

I have this problem where data values on my prototype dynamic UITableViewCell are reset when scrolling up or down.

I am checking for cell is nil as suggested in other threads since cells are reused but execution never hits that if.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    // Currency formatter
    let formatter = NSNumberFormatter()
    formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    formatter.locale = NSLocale(localeIdentifier: "el-GR")

    // Table view cells are reused and should be dequeued using a cell identifier.
    let cellIdentifier = "MenuItemTableViewCell"

    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! MenuItemTableViewCell

    if cell.isEqual(nil) {
        cell = UITableViewCell(style: .Default, reuseIdentifier: "MenuItemTableViewCell") as! MenuItemTableViewCell
    }

    // Match category (section) with items from data source
    let itemsPerSection = items.filter({ $0.category == self.categories[indexPath.section] })
    let item = itemsPerSection[indexPath.row]

    // cell data
    cell.cellTitle.text = item.name + "  " + formatter.stringFromNumber(item.price)!
    cell.cellDescription.text = item.description

Any help please?

Regards, Polis

Upvotes: 0

Views: 1181

Answers (1)

Charles A.
Charles A.

Reputation: 11123

Every time a cell goes off screen and back on again the cell is re-created. So if you modify the cell (i.e., as you describe with the stepper) you must save that modified value so that it is created with that modified value the next time it comes on screen.

Upvotes: 1

Related Questions