Sunday, January 23, 2022
HomeiOS Developmentios - Swift UITableView messing up after reloadData()

ios – Swift UITableView messing up after reloadData()

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:


enter image description here

Each cell of the table you can see leads to 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:


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! CellController
    let items =!.levels
    cell.model.text = items[indexPath.row].name
    if items[indexPath.row].id <! {
        cell.cost.text = "Complete"
        cell.cost.textColor = UIColor.systemGreen
        cell.accessoryType = .none
        cell.isUserInteractionEnabled = false
    } else if items[indexPath.row].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


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

            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?



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments