如何在表视图中以编程方式选择行? (快速4.0)

时间:2018-10-29 14:24:44

标签: swift uitableview

我有一个tableViewController,我的数据源运行良好,但无法以编程方式选择一行。

我尝试过:

class DocumentTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: UITableViewScrollPosition(rawValue: 0)!)
     }
}

当我运行时,tableView不会选择任何内容。 我该如何解决?

2 个答案:

答案 0 :(得分:2)

您的电话线很早就尝试了

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: UITableViewScrollPosition(rawValue: 0)!) 
}

viewDidLoad内的

tableView.reloadData()
tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: UITableViewScrollPosition(rawValue: 0)!)

答案 1 :(得分:0)

快速5:

tableView.selectRow(at: IndexPath(row: 0, section: 0), animated: true, scrollPosition: UITableView.ScrollPosition(rawValue: 0)!)