Vincent
Vincent

Reputation: 496

How to deselect a row when another row tapped?

Here is what I want to archive: deselect the "English" row when the second Chinese row tapped, or conversely.

Picture

I want only one checkmark to be shown, not all of them. Here is my code:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if tableView.cellForRow(at: indexPath)!.accessoryType == .checkmark  {
        tableView.cellForRow(at: indexPath)!.accessoryType = .none
        navigationItem.rightBarButtonItem!.isEnabled = false
    } else if tableView.cellForRow(at: indexPath)!.accessoryType == .none   {
        tableView.cellForRow(at: indexPath)!.accessoryType = .checkmark
        navigationItem.rightBarButtonItem!.isEnabled = true
    }
}

Upvotes: 0

Views: 95

Answers (1)

Vincent
Vincent

Reputation: 496

Finally, I got an answer. Here you go:

   func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)!.accessoryType = .checkmark
        navigationItem.rightBarButtonItem?.isEnabled = true
    }

    func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.cellForRow(at: indexPath)!.accessoryType = .none
        navigationItem.rightBarButtonItem?.isEnabled = false
    }

Upvotes: 1

Related Questions