0

I have a table I use that shows timezone options. I use a checkmark to show which one is currently selected. When the table is created I checkmark the cell that is saved as the users timezone.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cellData")

        switch indexPath.row {
            case 0: cell.textLabel?.text = "Eastern"
            case 1: cell.textLabel?.text = "Central"
            case 2: cell.textLabel?.text = "Mountain"
            case 3: cell.textLabel?.text = "Mountain (No DST)"
            case 4: cell.textLabel?.text = "Pacific"
            default: cell.textLabel?.text = ""
        }

        cell.selectionStyle = UITableViewCellSelectionStyle.None

        if(cell.textLabel?.text == keychain.get("timezone")) {

            cell.accessoryType = UITableViewCellAccessoryType.Checkmark

        }

        return cell
    }

Then I use these functions to change the checkmark when a user chooses a new timezone.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.cellForRowAtIndexPath(indexPath)!.accessoryType = UITableViewCellAccessoryType.Checkmark

}

override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.cellForRowAtIndexPath(indexPath)!.accessoryType = UITableViewCellAccessoryType.None

}

However, when I preset the checkmark it is not removed when I choose a new timezone. It will only work if I first select it and then choose a new one. Is there a reason the original cell is not affected on deselect?

Michael St Clair
  • 5,937
  • 10
  • 49
  • 75
  • My guess is that checking `accessoryType` is just a "mental selection" defined by you and it has nothing to do with the real selection. You might need to select a row by something like this: http://stackoverflow.com/questions/24787098/programmatically-emulate-the-selection-in-uitableviewcontroller-in-swift , only then `didDeselectRowAtIndexPath` will work for you. – Joe Huang Jan 31 '16 at 02:38
  • @JoeHuang when I use the code from that question I am getting an error that it is nil in didSelect – Michael St Clair Jan 31 '16 at 03:11

1 Answers1

1

The reason your original cell isn't being deselected is that it's not selected in the first place. Enabling the checkmark accessory doesn't select the cell.

It's a bit of a pain to set up, but one way you could get this working is by storing a reference to the cell that needs to be selected, and when the view is going to appear, select it manually. Then, deselections will work.

Add a class variable to remember the cell that should be selected

var initiallySelectedPath: NSIndexPath?

Set the variable in your cellForRowAtIndexPath (personally, I'd do this setting elsewhere in the class due to how the actual cell selection is going to be performed, but this is good enough to demonstrate a solution.

...
if (cell.textLabel?.text == keychain.get("timezone")) {
  cell.accessoryType = UITableViewCellAccessoryType.Checkmark
  initiallySelectedPath = indexPath
}
...

Then, in your viewWillAppear

override func viewWillAppear(animated: Bool) {
  super.viewWillAppear(animated)
  if let indexPath = initiallySelectedPath {
    // I force unwrap (sorry!) my tableView, you'll need to change this to however you reference yours
    tableView!.selectRowAtIndexPath(indexPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
  }
}

Now your original cell should deselect with the first tap on a different cell.

rickerbh
  • 9,731
  • 1
  • 31
  • 35
  • I didn't need to store it, I just used your line of code from view will appear inside the original table view method to make the cell selected. – Michael St Clair Jan 31 '16 at 03:15