【问题标题】:How to select a row programmatically in table View ? (Swift 4.0)如何在表格视图中以编程方式选择一行? (斯威夫特 4.0)
【发布时间】:2018-10-29 14:24:43
【问题描述】:

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

我试过了:

class DocumentTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

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

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

【问题讨论】:

标签: swift uitableview


【解决方案1】:

你的线很早,试一下

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)!)

【讨论】:

    【解决方案2】:

    在 Swift 5 中:

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

    【讨论】:

      猜你喜欢
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      • 2023-04-06
      • 2017-07-22
      • 2017-03-24
      相关资源
      最近更新 更多