Ishwar
Ishwar

Reputation: 73

how to uncheck uitableview cells using accessory checkmark

i have two sections

1.MapViewController

2.TypesTableViewController

when i run my app and call TypesTableViewController and when it opens it shows all cells selected i want it to be unchecked please help me and check my code

1.MapViewController

class MapViewController: UIViewController {

    @IBOutlet weak var mapCenterPinImage: UIImageView!
    @IBOutlet weak var pinImageVerticalConstraint: NSLayoutConstraint!
    var searchedTypes = ["bakery", "bar", "cafe", "grocery_or_supermarket", "restaurant"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "Types Segue" {
            let navigationController = segue.destinationViewController as! UINavigationController
            let controller = navigationController.topViewController as! TypesTableViewController
            controller.selectedTypes = searchedTypes
            controller.delegate = self
        }
    }
}

// MARK: - TypesTableViewControllerDelegate
extension MapViewController: TypesTableViewControllerDelegate {

    func typesController(controller: TypesTableViewController, didSelectTypes types: [String]) {
        searchedTypes = controller.selectedTypes.sort()
        dismissViewControllerAnimated(true, completion: nil)
    }

}

2.TypesTableViewController

protocol TypesTableViewControllerDelegate: class {
    func typesController(controller: TypesTableViewController, didSelectTypes types: [String])
}

class TypesTableViewController: UITableViewController {


    let possibleTypesDictionary = ["bakery":"Bakery", "bar":"Bar", "cafe":"Cafe", "grocery_or_supermarket":"Supermarket", "restaurant":"Restaurant"]
    var selectedTypes: [String]!
    weak var delegate: TypesTableViewControllerDelegate!
    var sortedKeys: [String] {
        return possibleTypesDictionary.keys.sort()
    }

    // MARK: - Actions
    @IBAction func donePressed(sender: AnyObject) {
        delegate?.typesController(self, didSelectTypes: selectedTypes)
    }

    // MARK: - Table view data source
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return possibleTypesDictionary.count
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("TypeCell", forIndexPath: indexPath) 
        let key = sortedKeys[indexPath.row]
        let type = possibleTypesDictionary[key]!
        cell.textLabel?.text = type
        cell.imageView?.image = UIImage(named: key)
        cell.accessoryType = (selectedTypes!).contains(key) ? .Checkmark : .None
        return cell
    }

    // MARK: - Table view delegate
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
        let key = sortedKeys[indexPath.row]
        if (selectedTypes!).contains(key) {
            selectedTypes = selectedTypes.filter({$0 != key})
        } else {
            selectedTypes.append(key)
        }

        tableView.reloadData()
    }
}

Upvotes: 2

Views: 1217

Answers (2)

hoss24
hoss24

Reputation: 556

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    //toggle checkmark on and off
    if tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    }
    else {
        tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
    }
    
    //add animation so cell does not stay selected
    tableView.deselectRow(at: indexPath, animated: true)
    
}

Upvotes: 2

Avaan
Avaan

Reputation: 4699

Not sure what you are doing in your code. If you want to uncheck then change below line to

cell.accessoryType = (selectedTypes!).contains(key) ? .Checkmark : .None

to

cell.accessoryType = (selectedTypes!).contains(key) ? . None : . Checkmark

Updated:- second part of the answer to get only checkmark cells,

change as below

@IBAction func donePressed(sender: AnyObject) {
   let rowCount = tableView.numberOfRowsInSection(0)
   selectedTypes.removeAll()
  for var index = 0; index < rowCount; ++index {
      let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as! YourCell
      if cell.accessoryType = .Checkmark{
         let key = sortedKeys[index]
         selectedTypes.append(key)
      }
    delegate?.typesController(self, didSelectTypes: selectedTypes)
  }
}

Upvotes: 1

Related Questions