Skip to content →

Programmatically selecting a row in a UITableView

I had a need to programmatically select a row in a UITableView. In order to do this, you have to do two things:

let indexPath = IndexPath(row: 0, section: 0)
self.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
self.tableView.delegate?.tableView!(self.tableView, didSelectRowAt: indexPath)

selectRow highlights the selected row and didSelectRowAt will call didSelectRowAt if you need to perform whatever action you defined.

For example code, see:

Published in iOS