是的,您可以在 swift 中执行此操作。只需像往常一样加载您的 tableViewController。您只需致电
在斯威夫特中
let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0); //slecting 0th row with 0th section
self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);
selectRowAtIndexPath 函数不会触发 didSelectRowAtIndexPath: 这样的委托方法,因此您必须手动触发此委托方法
self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect); //Manually trigger the row to select
或者,如果您使用 segue 推送 ViewControllers,则必须触发 performSegueWithIdentifier
self.performSegueWithIdentifier("yourSegueIdentifier", sender: self);
现在您可以在didSelectRowAtIndexpath 中推送viewController。选择第一部分的第一行或执行您在didSelectRowAtIndexpath: of tableView 中编写的任何内容
对于您的情况,只需将 viewController 推送到第 0 个索引处的选择 didSelectRowAtIndexPath
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
if indexPath.row == 0 {
self.navigationController.pushViewController(yourViewControllerToPush, animated: YES);
}
//handle other index or whatever according to your conditions
}
它将显示推送的视图控制器,如果您不想为视图控制器设置动画,只需将 NO 传递给 pushViewController 方法。
按下Back 按钮后,您将返回您的viewController
在您的情况下,只需将其写在您的viewDidAppear
if firstStart {
firstStart = false
let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0)
self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition:UITableViewScrollPosition.None)
self.performSegueWithIdentifier("showForecastSelectedCity", sender: self)
}