Instance Method

selectRow(at:animated:scrollPosition:)

Selects a row in the table view identified by index path, optionally scrolling the row to a location in the table view.

Declaration

func selectRow(at indexPath: IndexPath?, animated: Bool, scrollPosition: UITableView.ScrollPosition)

Parameters

indexPath

An index path identifying a row in the table view.

animated

true if you want to animate the selection and any change in position; false if the change should be immediate.

scrollPosition

A constant that identifies a relative position in the table view (top, middle, bottom) for the row when scrolling concludes. See UITableView.ScrollPosition for descriptions of valid constants.

Discussion

Calling this method does not cause the delegate to receive a tableView(_:willSelectRowAt:) or tableView(_:didSelectRowAt:) message, nor does it send selectionDidChangeNotification notifications to observers.

Special Considerations

Passing UITableView.ScrollPosition.none results in no scrolling, rather than the minimum scrolling described for that constant. To scroll to the newly selected row with minimum scrolling, select the row using this method with UITableView.ScrollPosition.none, then call scrollToRow(at:at:animated:) with UITableView.ScrollPosition.none.

NSIndexPath *rowToSelect;  // assume this exists and is set properly
UITableView *myTableView;  // assume this exists
 
[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];

See Also

Managing Selections

var indexPathForSelectedRow: IndexPath?

An index path identifying the row and section of the selected row.

var indexPathsForSelectedRows: [IndexPath]?

The index paths representing the selected rows.

func deselectRow(at: IndexPath, animated: Bool)

Deselects a given row identified by index path, with an option to animate the deselection.

var allowsSelection: Bool

A Boolean value that determines whether users can select a row.

var allowsMultipleSelection: Bool

A Boolean value that determines whether users can select more than one row outside of editing mode.

var allowsSelectionDuringEditing: Bool

A Boolean value that determines whether users can select cells while the table view is in editing mode.

var allowsMultipleSelectionDuringEditing: Bool

A Boolean value that controls whether users can select more than one cell simultaneously in editing mode.