Skip to content

Instantly share code, notes, and snippets.

@gchriswill
Created June 30, 2017 02:46
Show Gist options
  • Save gchriswill/7d168f35f7bc777812e04870170c0d3e to your computer and use it in GitHub Desktop.
Save gchriswill/7d168f35f7bc777812e04870170c0d3e to your computer and use it in GitHub Desktop.

Revisions

  1. gchriswill created this gist Jun 30, 2017.
    23 changes: 23 additions & 0 deletions UITableViewOnlyOneRowSelection.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    // A snippet for extending a UITableViewController class,
    // allowing the selection of only one row through the Checkmark accssory.
    // If you using a stand alone UITableView, then implement these methods in your delegate object.

    // For checking which row is selected, check the indexPathForSelectedRow property of the tableView.
    // This code is implemented in the latest version of Swift, which is Swift 3, as of today...

    extension <#Your UITableViewController class#> {

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let sip = selectedIndex {
    tableView.deselectRow(at: sip, animated: false)
    }
    selectedIndex = indexPath
    }

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    if selectedIndex?.row == indexPath.row {
    selectedIndex = nil
    }
    }
    }