我试图以编程方式选择tableview行。我一直在这里关注文档和其他引用,但是它不起作用。我没有收到错误,只是不选择行。

注意:目前,我只是尝试手动选择一行。

我是否正确使用了委托(delegate),数据源和NSIndexPath?

 override func viewDidLoad() {
        super.viewDidLoad()

    var myPath = NSIndexPath(forRow: 1, inSection: 0)
    myTableView.selectRowAtIndexPath(myPath, animated: false, scrollPosition: UITableViewScrollPosition.None)

swift - selectRowAtIndexPath无法快速运行-LMLPHP

最佳答案

在调用viewDidLoad时,表 View 可能未准备好或未填充。

尝试使用viewDidAppearviewWillAppear代替:

override func viewDidAppear(_ animated: Bool){
    let myPath = NSIndexPath(row: 1, section: 0)
    tableView.selectRow(at: myPath as IndexPath, animated: false, scrollPosition: UITableViewScrollPosition.none)
}

10-08 06:26