以编程方式在Swift中模拟UITableViewController中的select

对不起,没有任何代码行的问题。 如果可能,我需要build议如何进行。

在Swift语言中。

让我们假设有一个带有两个控制器的应用程序 – 带embedded式NavigationController UITableViewController作为主。 当你在表中select一行时,打开UIViewController ,它显示关于所选值的详细信息。

是否有可能做到这一点? 当应用程序启动时,以编程方式模拟表中第一行的select,并立即显示有关选定值的详细信息的UIViewController 。 从这个控制器可能通过NavigationController返回到UITableViewController

我再次道歉,并提前谢谢你!

是的,你可以做swift.Just加载你的tableViewController照常。你只需要打电话

在Swift中

  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:那样触发委托方法didSelectRowAtIndexPath:所以你必须手动触发这个委托方法

  self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect); //Manually trigger the row to select 

或者如果您使用segue推ViewControllers ,则必须触发performSegueWithIdentifier

  self.performSegueWithIdentifier("yourSegueIdentifier", sender: self); 

现在,您可以在didSelectRowAtIndexpath推送viewController。要select第一部分中的第一行或执行您在didSelectRowAtIndexpath:写入的whaterver didSelectRowAtIndexpath: tableView

至于你的情况只要按下didSelectRowAtIndexPath在0索引selectviewController

 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 } 

它将显示视图控制器推送,如果你不想animation视图控制器只是传递NO pushViewController方法。

按下Backbutton,你会回到你的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) } 

你也可以通过访问tableView委托来强制“didSelectRowAtIndex”来触发

//以编程方式select一行来处理

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

/ /编程select行不会触发“didSelectRowAtIndexPath”函数,所以我们必须手动强制射击按照下面

 self.tableView.delegate!.tableView!(tableView, didSelectRowAtIndexPath: indexPath!)