Detect when a user taps a table view cell so your app can take the next indicated action.
When a user taps a row of a table view, an action of some kind usually follows, such as another view sliding into place, or the row indicating the selected state with a checkmark. In order for your app to perform the action, it must know when the user taps the row and respond accordingly.
Configure Row Selection Behavior
Use the following properties to configure how row selection behaves in a table view:
Determines whether users can select a row when the table isn’t in editing mode. The default is
Determines whether users can select more than one row when the table isn’t in editing mode. The default is
Selection During Editing
Determines whether user can select a row while the table view is in editing mode. The default is
Multiple Selection During Editing
Determines whether users can select a more than one row while in editing mode. The default is
Respond to Row Selections
When the user taps a row, the table view calls the delegate method
table. At this point, your app performs the action, such as displaying the details of the selected hiking trail:
If you respond to the cell selection by pushing a new view controller onto the navigation stack, deselect the cell when the view controller pops off the stack. If you’re using a
UITable to display a table view, you get the behavior by setting the
clears property to
true. Otherwise, you can clear the selection in your view controller’s
If the row displays the detail button accessory view and the user taps it, the table view calls the
table method instead of calling the ”did select row” method. To learn more about accessory views, see Configuring the Cells for Your Table.
Select a Row Programmatically
Occasionally the selection of a row originates within the app itself rather than from a tap in the table view. For example, the user adds a new person to an address book and returns to the list of contacts. The app then scrolls this list to the recently added person. In this situation, you can use the table view methods
select and—if the row is already selected—
scroll. You may also call
scroll when you want to scroll to a specific row without selecting it.
Manage Selection Lists
You can also use cell selection to maintain inclusive and exclusive lists of selected items. An inclusive list can have one or more selected items while an exclusive list has at most one.
When providing a selection list in your app, don’t use the cell’s selected state to indicate the state of the item. Instead, display a checkmark or an accessory view. To indicate the state using a checkmark, implement the
table delegate method, then deselect the cell with
deselect, followed by setting the cell's
accessory property to
For example, an app for backpackers may let users create a packing list—an inclusive list of camping gear like sleeping bags and tents. When the user taps an item to indicate that they packed that piece of gear, the app unselects the row, updates the item's data model, and displays a checkmark in the row for packed items. Tapping the item again marks the item as unpacked and removes the checkmark. Here’s an example:
Managing an exclusive list is similar. Deselect the row and display a checkmark or an accessory view to indicate the selected state. But unlike an inclusive list, limit the exclusive list to only one selected item at a time.
For instance, the backpacker's app may let users filter the list of hiking trails based on a single difficultly level: easy, moderate, and hard. With an exclusive list such as this, the app must remove the checkmark from the previous selection, and display a checkmark for the current selection. The app must also remember which item is the currently selected item. For example: