Hey guys so I developed an NoteApp, the wanted behavior is that whenever a user taps on one of the cells a checkmark appears and if tapped again the checkmark disappears . Here is what my code looks like.
Item.swift
class Item: Object {
@objc dynamic var title: String?
@objc dynamic var done: Bool = false
}
ViewController.swift
var toDoItems: Results<Item>!
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = toDoItems[indexPath.row].title
cell.accessoryType = toDoItems[indexPath.row].done ? .checkmark : .none
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
try! realm.write {
toDoItems![indexPath.row].done = !toDoItems![indexPath.row].done
}
tableView.reloadData()
tableView.deselectRow(at: indexPath, animated: true)
}
So the problem is that whenever I tap on a cell the checkmark does not appear and my realm done property remains unchanged in Realm Studio . Please help!!