Ошибка возникает из-за того, что вы переключаете состояние checked
всегда в configureCheckmark
. Поэтому, когда для этой строки вызывается cellForRow
, состояние переключается.
На самом деле дополнительный метод configureCheckmark
не нужен. Поставьте строку, чтобы установить галочку в cellForRow
, но не меняйте состояние.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistItem", for: indexPath)
let item = todoList.items[indexPath.row]
configureText(for: cell, with: item)
cell.accessoryType = item.checked ? .checkmark : .none
return cell
}
В didSelectRowAt
переключите checked
в модели и перезагрузите строку
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
todoList.items[indexPath.row].checked.toggle()
tableView.reloadRows(at: [indexPath], with: .none)
}
Метод toggleChecked()
в ChecklistItem
также является избыточным. В Bool
.
есть метод
toggle()
.
И подумайте об использовании структур, а не классов.