Search code examples
swift3tableviewrowdeselect

Cannot deselect previous programmatically selected row in tableview after another selection


I am using both in cellForRowAt and exact in the same function that initialize the tableview this code:

cell.accessoryType = selectedOption == indexPath.row ? .checkmark : .none

and my tableview got checkmark automatically for selectedOption, but when I select another option the checkmark that I set earlier doesn't disapear and after that there are 2 checkmarks on the table with setted allowsMultipleSelection to false. Why this is happening and why my first checkmark doesn't hide after my selection ?

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     if let cell = tableView.cellForRow(at: indexPath) {
                cell.accessoryType = .checkmark
     }
}

Solution

  • I think if you reload your tableview it will works, your code is fine, just reload your table in didselect.