表视图单元格突出显示无关紧要什么

Nic*_*ari 1 uitableview uistoryboardsegue swift

UITableViewController当用户点击其中一个表格视图单元格时,我的基于故事板的子类将另一个视图控制器推送到导航中.

我使用segue来实现这一点,而不是以编程方式进行pushViewController(:animated:).

我遇到的问题是我的表视图单元格一直保持高亮显示(浅灰色),即使在弹出视图控制器之后(再次使用'展开'segue).清除它的唯一方法是打电话tableView.reloadData().


我试过了:

  1. 设置:

    self.clearsSelectionOnViewWillAppear = true
    
    Run Code Online (Sandbox Code Playgroud)

    in viewDidLoad()(不应该是必需的,因为它已经是默认值:模板代码已self.clearsSelectionOnViewWillAppear = false注释掉,并建议取消注释以覆盖默认功能).

  2. 也:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        if let indexPath = tableView.indexPathForSelectedRow {
            tableView.deselectRow(at: indexPath, animated: true)
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)

    ......无济于事

    编辑:原来viewDidAppear()叫上导航的流行,只是被添加到视图层次结构(例如,后addSubview()).放置此代码viewWillAppear() 可以解决问题.但是我在这篇文章末尾的问题仍然存在.

  3. 我也试过实现这个方法:

    override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    
    Run Code Online (Sandbox Code Playgroud)

    ...但它没有被调用(可能是因为segue?)

成功取消单元格的唯一事情就是调用:

tableView.deselectRow(at: indexPath, animated: true)
Run Code Online (Sandbox Code Playgroud)

...从内prepareForSegue().但这种情况发生在推送上,我想在pop上取消突出显示我的单元格.

我是否需要为push(cell tap)segue设置专用的展开方法,并在那里取消强光?

为什么它不是开箱即用的,即使我是子类UITableViewController

Zac*_*wan 6

就像你提到的,使用 tableView.deselectRow(at: indexPath, animated: true)

但是,而不是在:

override func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    tableView.deselectRow(at: indexPath, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

你应该把它放进去:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    // You other cell selected functions here ...
    // then add the below at the end of it.
    tableView.deselectRow(at: indexPath, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

didDeselectRowAt在取消选择行之前不会调用.因此,为了取消选择单元格,您应该将其添加到单元格中didSelectRow.这样,无论何时选择单元格并didSelectRow获得触发器,您都将运行其他代码以及de-select最后的单元格.