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
}
-
—
OOPer
-
—
Junaid6305
Add a CommentYou are using
meterNumberwhich is not defined in your shown code. Usually, having other variables than data source can easily cause issues as you describe. Also, you are settingtextColorto.systemRed, but there is no code which changestextColorto the normal color, please do not forget that cells are reused, thetextColorof thecelldequeued is not known. Anyway, to find how to fix, you may need to show more context.Sorry that was a typo in my question. “meterNumber” is supposed to be “name”