How to use UIStepper and UITableViewCell?

I want to use a UIStepper in each of my tableview rows which updates a count in that tableview row. For some reason my implementation right now updates the tableview row label count but for some reason it also updates the label a couple of rows down as well.


This code is in the viewcontroller

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = self.tableview.dequeueReusableCell(withIdentifier: "add-item", for: indexPath) as! InventoryTableViewCell
      
        cell.productLabel.text = inventoryObject[indexPath.section].sectionItems[indexPath.row]
        cell.priceLabel.text = String(inventoryObject[indexPath.section].sectionPrices[indexPath.row])
  
        return cell
    }


This code is in the inventorytableview cell which each outlet is connected to the corresponding component.

import UIKit
class InventoryTableViewCell: UITableViewCell {
    @IBOutlet var productLabel: UILabel!
    @IBOutlet var priceLabel: UILabel!
    @IBOutlet var quantityStepper: UIStepper!
    @IBOutlet var quantityLabel: UILabel!
   
   
    override func awakeFromNib() {
        super.awakeFromNib()
        /
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        /
    }
    @IBAction func quantityStepperAction(_ sender: UIStepper) {
        quantityLabel.text = sender.value.description
    }
}


Any help would be great thanks.

Hi,


you need to initialize the value of quantityStepper each time a cell is dequeued. You're just setting the lable's text so the value of the UIStepper is where is was when the cell was used last. So if a cell is scrolled out of the tableView's frame it gets removed and put into the reuse queue. The next time it is dequeued the stepper stays on the same value as is was when the cell was put into the reuse queue.


But I'm not sure if this is the cause for the issue you've described.


Dirk

How to use UIStepper and UITableViewCell?
 
 
Q