UITableView does not automatically deselect the selected row when the table re-appears

前端 未结 7 594
感动是毒
感动是毒 2021-02-03 17:45

Normally a selected row in a UITableView gets deselected with an animation when the user pops back from the detail view.

However, in my case where I have a

7条回答
  •  终归单人心
    2021-02-03 18:25

    In swift, you can add the following lines in your viewWillAppear

    if let row = tableView.indexPathForSelectedRow() {
        tableView.deselectRowAtIndexPath(row, animated: true)
    }
    

    In swift 2, it's without parantheses:

    if let row = tableView.indexPathForSelectedRow {
        tableView.deselectRowAtIndexPath(row, animated: true)
    }
    

    In Swift 4 (and 3?) the function name was cleaned up:

    if let indexPath = tableView.indexPathForSelectedRow {
        tableView.deselectRow(at: indexPath, animated: true)
    }
    

提交回复
热议问题