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.
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
}
}