如何在不使用UITableViewController时更改clearsSelectionOnViewWillAppear?

pix*_*eak 17 objective-c ipad ios

我有一个UIViewController来管理UISearchBar和UITableView.我已经读过Apple不鼓励让多个UIViewControllers管理你应用程序的一部分,所以我没有使用UITableViewController来管理UITableView.相反,我在我自己的UIViewController中实现了UITableViewDelegateUITableViewDataSource协议.

我的问题是,由于我不再使用UITableViewController,我如何实际改变clearsSelectionOnViewWillAppear行为?该属性是UITableViewController的一部分.

omz*_*omz 47

只需打电话

[myTableView deselectRowAtIndexPath:[myTableView indexPathForSelectedRow] animated:YES];
Run Code Online (Sandbox Code Playgroud)

在你的viewWillAppear:方法.


Dar*_*rko 5

这里的Swift代码:

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