skip to Main Content

I’m developing an iOS app where I want to reload information of a UITableView from a different controller. In order to do that, I have a reference of the controller MainViewController with the UITableView in another controller called (AirlinesController).

I have an issue reloading the data of the UITableView of the first controller where it messes up pretty much:

MainViewController

enter image description here

Each cell of the table you can see leads to AirlinesController:

AirlinesController

enter image description here

So after the user clicks on the "Apply" button, the UITableView of MainViewController reloads using mainView.reloadData(). In this example I would want to see in the MainViewController that the cell with the title "Embraer" has a green label with the text "Completed" and the cell with the title "Airbus" has a yellow label with the title "Employee" but this is the result I get after reloading the table:

enter image description here

Why did the last cell of the table change one of its labels color to yellow?

This is the code I’m using:

MainViewController

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! CellController
    
    let items = self.gameView.data!.levels
    
    cell.model.text = items[indexPath.row].name
    
    if items[indexPath.row].id < self.gameView.game!.level.id {
        cell.cost.text = "Complete"
        cell.cost.textColor = UIColor.systemGreen
        cell.accessoryType = .none
        cell.isUserInteractionEnabled = false
    } else if items[indexPath.row].id == self.gameView.game!.level.id {
        cell.cost.text = "Employee"
        cell.cost.textColor = UIColor.systemYellow
        cell.accessoryType = .none
        cell.isUserInteractionEnabled = false
    } else {
        cell.cost.text = "(items[indexPath.row].XP) XP"
    }

    return cell
}

AirlinesController

@IBAction func apply(_ sender: Any) {
    if (mainView.game.XP >= level.XP) {
        let new_salary = Int(Float(mainView.game.salary) * level.salaryMultiplier)
        let new_XP = Int(Float(mainView.game.XPSalary) * level.XPMultiplier)
        mainView.game.salary = new_salary
        mainView.game.XPSalary = new_XP
        mainView.salaryLabel.text = "(new_salary) $"
        mainView.XPLabel.text = "(new_XP) XP"
        
        mainView.workingFor.text = "Currently working for (level.name)"
        
        mainView.game.level = Level(id: level.id, name: level.name, XP: 0, XPMultiplier: 1, salaryMultiplier: 1, aircrafts: [Aircraft]())
        
        mainView.saveGame()
        
        DispatchQueue.main.async {
            self.mainView.levelItems.reloadData()

            self.navigationController?.popViewController(animated: true)
        }
    } else {
        let alert = UIAlertController(title: "Error", message: "Not enough experience to apply!", preferredStyle: UIAlertController.Style.alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.cancel, handler: nil))

        self.present(alert, animated: true)
    }
}

How can I fix this?

2

Answers


  1. This is because of the reuse of cells.

    Set the default color in else condition.

    } else {
      cell.cost.textColor = UIColor.gray
      cell.cost.text = "(items[indexPath.row].XP) XP"
    }
    

    Another way, you can set the default style property inside the prepareForReuse method of UITableViewCell

    class TableViewCell: UITableViewCell {
        override func prepareForReuse() {
            super.prepareForReuse()
            // Set default cell style
        }
    }
    
    Login or Signup to reply.
  2. TableViewCell are reused all the time. My advice is that you have to set the default color every time.

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! CellController
    
    cell.cost.textColor = ...your default text color....
    
    ...
    return cell 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search