I am trying to select tableview rows programatically. I've been following the documentation and other reference here but it is not working. I don't get an error, it just do not select the rows.
Note: at the moment I am just trying to select one row manually.
Am I using the delegates, datasource and NSIndexPath correctly?
override func viewDidLoad() {
super.viewDidLoad()
var myPath = NSIndexPath(forRow: 1, inSection: 0)
myTableView.selectRowAtIndexPath(myPath, animated: false, scrollPosition: UITableViewScrollPosition.None)
It is likely that the table view is not ready or not populated at the time viewDidLoad
is called.
Try it in viewDidAppear
or viewWillAppear
instead:
override func viewDidAppear(_ animated: Bool){
let myPath = NSIndexPath(row: 1, section: 0)
tableView.selectRow(at: myPath as IndexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With