techcoderx
techcoderx

Reputation: 602

How to save the state of UITableViewCell accessory checkmark in Swift 3

I have a table view and I would like the user to select only one item from the table view, then I want to add a checkmark to the cell. At the same time, I also want the checkmark on other cells to disappear so the. user can only select one at a time. Then I want to save the data (preferably using UserDefaults). How do I do that?

Upvotes: 0

Views: 872

Answers (2)

Sagar vaishnav
Sagar vaishnav

Reputation: 97

you should Use in this way

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

Upvotes: 0

Bharath Reddy
Bharath Reddy

Reputation: 746

Create a class variable of type IndexPath. In your cellForRowAt method set .checkmark accessory type only if index path matches the class variable and .none otherwise. In didSelectRowAt method update the indexPath of selected variable and just reload the tableview

Upvotes: 1

Related Questions