In my Table View I have the label text set to a different colour if the name is in the Flagged Array (flaggedNames). The correct cell labels turn red but as I scroll the text of other labels are also red. This changes as I scroll.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let name = names[indexPath.row]
cell.textLabel?.text = name
if flaggedNames.contains(meterNumber) {
cell.textLabel?.textColor = .systemRed
}
return cell
}
Cells can be reused, which can lead to such problems. To this end, you should specify the text color of the normal label in the 'else' statement.
if flaggedNames.contains(meterNumber) {
cell.textLabel?.textColor = .systemRed
} else {
cell.textLabel?.textColor = .black
}