Skip to content

Instantly share code, notes, and snippets.

@RNHTTR
Created September 23, 2017 20:01
Show Gist options
  • Save RNHTTR/1adcf478109c31b6d141deba94048d32 to your computer and use it in GitHub Desktop.
Save RNHTTR/1adcf478109c31b6d141deba94048d32 to your computer and use it in GitHub Desktop.
An example of how to use tableView(_:didSelectRowAt:) to manage what happens when users select a UITableViewCell
// Use tableView(_:didSelectRowAt:) to manage what happens when a user selects a UITableViewCell.
// This example exhibits how this app makes use of this method.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Determine what to do when a cell in a particular section is selected.
switch indexPath.section {
case 0:
// If the cell in the first section is selected, this will trigger the segue with the "toHeightForRowAt" identifier
self.performSegue(withIdentifier: "toHeightForRowAt", sender: nil)
case 1:
// Create an alert if the cell in the "Managing Selections" section is selected.
let alert = UIAlertController(title: "Alert", message: "This alert is fired when you select the didSelectRowAt cell.", preferredStyle: .alert)
// Create an action do dismiss the Alert Controller.
alert.addAction(UIAlertAction(title: "Dismiss", style: .default, handler: nil))
// Create an action that triggers a segue with the identifier "toDidSelectRowAt". That's how you got to this web
// page!
alert.addAction(UIAlertAction(title: "Continue to GitHub to see how this is done.", style: .default, handler: { _ in
self.performSegue(withIdentifier: "toDidSelectRowAt", sender: nil)
}))
// Don't forget to present the alert controller!
self.present(alert, animated: true, completion: nil)
case 2:
// If the cell in the third section is selected, this will trigger the segue with the "toEditActionsForRowAt"
// identifier
self.performSegue(withIdentifier: "toEditActionsForRowAt", sender: nil)
default:
print("Out of index")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment