Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

selectRowAtIndexPath not working in swift

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)

enter image description here

like image 323
GuiSoySauce Avatar asked Aug 13 '15 09:08

GuiSoySauce


1 Answers

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)
}
like image 70
idmean Avatar answered Nov 20 '22 15:11

idmean