Swift 更改后选择一行 viewcontroller

Swift leave a row selected after changing viewcontroller

我有两个视图应用程序。如果我 select 一行 segue 加载第二个视图控制器。我希望当我 return 回到我的第一个控制器时,该行仍然是 selected。我试过这个:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    tableView.cellForRowAtIndexPath(indexPath)!


   self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.None)

}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
           self.tableView.deselectRowAtIndexPath(indexPath, animated: true)


}

但是当 returning 返回时,该行不会保留 selected。有什么问题?? P.S.: 我不需要多个 selection,一次只需要一行。

如果您使用 UITableViewController 子类作为源视图控制器,我认为是因为您正在覆盖 table 视图委托方法,您可以在视图控制器上使用标志 clearsSelectionOnViewWillAppear 来得到这个行为。

override func viewDidLoad() {
    self.clearsSelectionOnViewWillAppear = false
}

Or you can set this in your Storyboard as well, when selecting the attributes selector of your source table view controller, you have to untick the Clear on Appearance复选框。