设置默认单元格以在 UIableView 中突出显示

Set a default cell to be highlighted in UIableView

我有一个拆分视图,在主视图中有 table 视图。 在第一次打开时,来自 master 的其中一个单元格将显示在详细信息页面中。 我想,当它打开时,它的单元格应该突出显示。例如,在 iPad 上的 iOS 设置中,当您打开它时,"General" 单元格会突出显示并在详细信息页面中打开。

viewDidLoad,我写的,有点不行

override func viewDidLoad() {
    super.viewDidLoad()
    let indexPath = IndexPath(row: 0, section: 0)
    tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
}

你能帮帮我吗?

UITableViews/UICollectionViews 在 vi​​ewDidLoad() 中几乎为零,加载视图后,您将能够在 tableView 中突出显示单元格。

您可以:

   override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(true)

    DispatchQueue.main.async {

    let indexPath = IndexPath(row: 5, section: 0)
    self.tableView.selectRow(at: indexPath, animated: true, scrollPosition: .none)
     }
}

 override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    let indexPath = IndexPath(row: 5, section: 0)
 tableView.selectRow(at: indexPath, animated: true,   scrollPosition: .none)

}