Custom Cell - button in cell updates multiple cells

I have a table view with custom cells. The cell consists of two buttons and a label. The buttons is a pluss button, and a minus button. The label is used for counting buttonpresses. When the pluss button is pressed, the label should increment by one, and decrease by one when the minus button is pressed.
The problem is that the buttons updates multiple cells. How do I get just that one label in the corresponding cell to update?

Code:

Custom Cell File:

import UIKit

class CustomTableViewCell: UITableViewCell {
    
    var counter: Int = 0 {
        didSet {
            counterLabel.text = String(counter)
        }
    }
    
    @IBAction func plussButton(_ sender: UIButton)
    {
        counter += 1
    }
    @IBAction func minusButton(_ sender: UIButton)
    {
        counter -= 1
    }
    
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var counterLabel: UILabel!
    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

TableViewController:

import UIKit



class UnderMenyTableViewController: UITableViewController {

    var nameArray = ["Cell 1", "Cell 2", "Cell 3", "Cell 4", "Cell 5" ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return nameArray.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath)
        
        if let customCell = cell as? CustomTableViewCell
        {
            customCell.nameLabel.text = nameArray[indexPath.row]
            customCell.counterLabel.text = String (customCell.counter)
            
        }
        
        
        return cell
    }
    
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 200
    }
}

Thanks.

dequeueReusableCell will reuse cells that have scrolled off the top or bottom of the view, so you will only get enough cells to fill the view. When they get reused, they will be like they were when they scrolled off, with whatever values they had at the time, until you reset them.

I think you need to store the counters in an array, and retrieve them using indexPath.row, the way you are doing with the names.

Exaclty, you should store the counter in a model class, that you will use to populate your tableview.

This topic was automatically closed after 166 days. New replies are no longer allowed.