I have a given number of cells in a section. My goal is to have only the last selected cell to display a checkmark. The other cells should not.
I have found a function in this similar but older thread. I've modified it (and I bet here lies the problem) slightly because of the changes in Swift 3.0.
As it is written below, for me, the function does not work properly. Only the last cell (not last selected, but last in section) in the section will ever get the checkmark. But I cannot figure out why not.
Here is the full function:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let section = indexPath.section
let numberOfRows = tableView.numberOfRows(inSection: section)
for row in 0..<numberOfRows {
if let cell = tableView.cellForRow(at: indexPath) {
cell.accessoryType = row == indexPath.row ? .checkmark : .none
}
}
}
By printing out the values I can see when this statement below evaluates to true, and it makes sense. But the checkmark does not get toggled.
cell.accessoryType = row == indexPath.row ? .checkmark : .none
Thank you!