How to delete (reset) all selected multiple checkmarks?

250 Views Asked by At

I have a tableView with a multiple selection with accessoryType checkmarks.

Then i have a „Reset All“ Button in the Navigation Bar. I want to clear (remove/reset) ALL checkmarks with this Button.

First the Struct and Array I made:

struct Area {
let name : String
var isSelected : Bool
init(name : String, isSelected : Bool = false) {
self.name = name
self.isSelected = isSelected
} }

var areas = [Area(name: "Name1"), Area(name: "Name2"), Area(name:"Name3"), Area(name: "Name4"), Area(name: "Name5")]

At "didSelectRowAt" i have this:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
areas[indexPath.row].isSelected.toggle()
tableView.reloadRows(at: [indexPath], with: .none) }

At "cellForRowAt" i have this:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = myTableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyTableViewCell

let area = areas[indexPath.row]
    cell.textLabel?.text = area.name
    cell.accessoryType = area.isSelected ? .checkmark : .none }

Then I have a "Reset All" Button in my Navigation Bar:

 @IBAction func resetButtonTapped(_ sender: UIBarButtonItem)
{//...}

With this button I want to reset all selected checkmarks. I don't know how to do this.

Can someone help me how this will works?

1

There are 1 best solutions below

3
Emre Metilli On

You have to set all item's isSelected bool in the areas array. After that, you can call tableView.reloadData() function. You must do it in resetButtonTapped function.

@IBAction func resetButtonTapped(_ sender: UIBarButtonItem){
    for area in areas
    {
        area.isSelected = false
    }
    yourTableView.reloadData()
}