Comment changer la couleur d'une cellule après un moveRowAt ?

Bonjour,
Je souhaite que la couleur de la cellule d’une tableView change en fonction de la section dans laquelle elle se trouve après un déplacement (moveRowAt).
Il semble que la fonction moveRowAt soit appelée avant l’affichage => ce n’est pas la bonne cellule qui change de couleur. on voit que c’est le patient 11 qui est grisé alors que cela devrait être le patient 01.

0431

Nota : le code ne gère que l’affichage, pas les données.

mon code :
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) → Int {
let taskCount: Int = 2
return taskCount
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    print("in cellForRowAt")
    let cell = tableView.dequeueReusableCell(withIdentifier: "patient-cell")!
        cell.textLabel?.text = "patient \(indexPath.section)\(indexPath.row)"
        cell.accessoryType = .checkmark

// cell.accessoryType = .none

    return cell
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
    var titleHeader:String = ""
    if section == 0 {
        titleHeader = "A faire"
    } else if section == 1 {
        titleHeader = "Ne pas faire"
    }
    return titleHeader
}

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return .none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
    return true
}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    print("in willDisplay")
    if indexPath.section == 0 {
        cell.backgroundColor = UIColor.white
    }
    if indexPath.section == 1 {
        cell.backgroundColor = UIColor.lightGray
    }
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    print("in moveRowAt")

    if destinationIndexPath.section == 1 {
        tableView.cellForRow(at: destinationIndexPath)?.backgroundColor = UIColor.lightGray
    } else {
        tableView.cellForRow(at: destinationIndexPath)?.backgroundColor = UIColor.white
    }
}

Salut Gilles,

Essaye ça (peut être) ,

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
print("in moveRowAt")

if destinationIndexPath.section == 1 {
    tableView.cellForRow(at: destinationIndexPath)?.backgroundColor = UIColor.lightGray
} else {
    tableView.cellForRow(at: destinationIndexPath)?.backgroundColor = UIColor.white
}
  // Recharger la cell 
 tableView.reloadRows(at: [destinationIndexPath], with: .none)
}

Merci Samir, mais j’ai l’erreur suivante :
reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

c’est bizarre, il voit 0 move ! et il semble se mélanger entre les sections.

Merci Samir pour la piste.

En rajoutant la gestion des données, cela fonctionne avec tableView.reloadData mais pas avec tableView.reloadRows.

Est-il nécessaire de reloader toutes les Data pour que cela fonctionne ? ou il y a-t-il une façon plus légère ?

Je suis bien content que tu es trouvé la solution, je pense pas que le fait de recharger la table soit un problème en soit, l’essentiel étant que celà fonctionne. :slight_smile: