以编程方式模拟Swift中UITableViewController中的选择

Ale*_*mov 10 uitableview ios swift

对不起任何代码行的问题很抱歉.如果可能的话,我需要建议如何继续.

用Swift语言.

让我们假设有一个带有两个控制器的应用程序 - UITableViewController以嵌入式NavigationController为主.在表中选择一行时,将打开UIViewController,其中显示有关所选值的详细信息.

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

我再一次道歉并提前感谢你!

cod*_*ter 40

是的,你可以在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);
Run Code Online (Sandbox Code Playgroud)

selectRowAtIndexPath函数不会触发委托方法,didSelectRowAtIndexPath:因此您必须手动触发此委托方法

   self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect); //Manually trigger the row to select
Run Code Online (Sandbox Code Playgroud)

或者如果你ViewControllers用segue 推动,你必须触发performSegueWithIdentifier

    self.performSegueWithIdentifier("yourSegueIdentifier", sender: self);
Run Code Online (Sandbox Code Playgroud)

现在,你可以把的viewController中didSelectRowAtIndexpath的第一部分.要选择第一行或执行您世袭的都写在 didSelectRowAtIndexpath: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
}
Run Code Online (Sandbox Code Playgroud)

它将显示推送的视图控制器,如果您不想为视图控制器设置动画,只需传递NOpushViewController方法.

按下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)
}
Run Code Online (Sandbox Code Playgroud)


小智 6

您也可以通过访问tableView Delegate来强制执行"didSelectRowAtIndex"来执行此操作

//以编程方式选择要使用的行

self.tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: UITableViewScrollPosition.Top)
Run Code Online (Sandbox Code Playgroud)

//以编程方式选择行不会触发"didSelectRowAtIndexPath"函数,因此我们必须手动强制执行以下操作

self.tableView.delegate!.tableView!(tableView, didSelectRowAtIndexPath: indexPath!)
Run Code Online (Sandbox Code Playgroud)