Alexander
Alexander

Reputation: 167

Reset stepper in UITableViewCell (Swift)

I have problem with my UIStepper. I do a counting game and for each time I press "new Competition" I want the stepper to reset the value. I have tried a lot of different way without no result.

What should I do?

class GameRoundTableViewCell: UITableViewCell {

    @IBOutlet weak var scoreLabel: UILabel!
    @IBOutlet weak var stepper: UIStepper!


    var index: IndexPath?

    var player : Player?

    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
    }
    @IBAction func StepperCounter(_ sender: UIStepper) {
        player?.score = Int(sender.value)
        scoreLabel.text = String(Int(sender.value))

    }

}

class GameViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet weak var previewButton: UIButton!
    var currentCompetitionsIndex = 0
    @IBOutlet weak var nextCompetition: UIButton!
    var gameRoundTableViewcell = "GameRoundTableViewcell"
    @IBOutlet weak var tabelViewGameRound: UITableView!
    let gameCellId = "GameCellId"
    var players : [Player]? = []
    var scoreLabel2 = GameRoundTableViewCell()
    var stepper2 = GameRoundTableViewCell()

    var questions : [Comepetitions]?
    @IBOutlet weak var textFieldCompInfo: UITextView!
    @IBOutlet weak var headingLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        nextCompetition.layer.cornerRadius = 26
        nextCompetition.clipsToBounds = true

        if let competition = questions?[currentCompetitionsIndex] {
            textFieldCompInfo?.text = competition.comepetitionsInfo
            headingLabel.text = competition.comepetitionsOption
        }
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return players!.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  {
        let cell = tableView.dequeueReusableCell(withIdentifier: gameCellId, for: indexPath as IndexPath) as! GameRoundTableViewCell

        cell.textLabel?.text = players?[indexPath.row].name
        if let score = players?[indexPath.row].score {
            cell.scoreLabel?.text = String(score)
            // cell.scoreLabel?.text =  "\(String(describing: person.score))"
        }
        cell.player = players?[indexPath.row]

        return cell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 70
    }
    func refresh(){
        tabelViewGameRound.reloadData()
    }


    @IBAction func previewCompeition(_ sender: UIButton)  {

        guard let quest = questions else {return}

        if currentCompetitionsIndex - 1 < quest.count  {
            currentCompetitionsIndex -= 1
            textFieldCompInfo.text = quest[currentCompetitionsIndex].comepetitionsInfo
            headingLabel.text = quest[currentCompetitionsIndex].comepetitionsOption

        } else {
            currentCompetitionsIndex = 0
            performSegue(withIdentifier: "resultSegue", sender: nil)
        }
        if currentCompetitionsIndex == 0{
            previewButton.isHidden = true
        }
    }

**//When I Press this button I want to reset the stepper.**

    @IBAction func newCompetition(_ sender: UIButton)  {

        guard let players = players else {return}

        for player in players {
            player.scoreForEachRound.append(player.score)
            player.score = 0
        }
        tabelViewGameRound.reloadData()
        guard let quest = questions else {return}

        if currentCompetitionsIndex + 1 < quest.count  {
            currentCompetitionsIndex += 1
            textFieldCompInfo.text = quest[currentCompetitionsIndex].comepetitionsInfo
            headingLabel.text = quest[currentCompetitionsIndex].comepetitionsOption
        } else {
            currentCompetitionsIndex = 0
            performSegue(withIdentifier: "resultSegue", sender: nil)
        }
        if currentCompetitionsIndex > 0{
            previewButton.isHidden = false
        }

    }
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let vc = segue.destination as! FinalResultViewController
        vc.finalResualt = self.players
    }
}

Upvotes: 0

Views: 233

Answers (1)

Andreas Oetjen
Andreas Oetjen

Reputation: 10199

I assume you observe the following behavior that the display in the scoreLabel gets out of sync with the stepper control. This missing sync occurs in two situations:

  • When you scroll in the table view (reuse of dequeued cells)
  • When you reset all the player's scores

The reason for this is that you only update the scoreLabel, but do not reset the stepper. Both controls are independend from each other, e.g. resetting the one will not influence the other. Therefore the stepper keeps the old (invalid) value, and when it's pressed, it will continue updating the label with that value.

You need to do the following: in tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath), also set the stepper to the current count:

if let score = players?[indexPath.row].score {
    cell.scoreLabel.text = String(score)
    cell.stepper.value = score
}

Upvotes: 1

Related Questions