以编程方式模拟Swift中UITableViewController中的选择 [英] Programmatically emulate the selection in UITableViewController in Swift

查看:90
本文介绍了以编程方式模拟Swift中UITableViewController中的选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Sorry for the question without any line of code. I need advice how to proceed, if at possible that I want.

用Swift语言。

让我们假设有一个带有两个控制器的应用程序 - UITableViewController 带有嵌入式 NavigationController ,作为主要内容。当您在表格中选择一行时,打开 UIViewController ,它会显示有关所选值的详细信息。

Let us assume there is an app with two controllers - UITableViewController with embedded NavigationController, as the main. When you select a row in the table, opens UIViewController, which displays detailed information about the selected value.

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

Is it possible to do otherwise? When the application start, programmatically emulate the selection of the first row in the table and immediately display UIViewController with detailed information about the selected value. And from this controller possible to return to UITableViewController via the NavigationController.

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

Once again I apologize and thank you in advance!

推荐答案

是的,你可以在swift中做到这一点。只需加载你的 tableViewController 像往常一样。你只需要打电话

Yes you can do this in swift.Just load your tableViewController as usual.You just have to call

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

The selectRowAtIndexPath function will not trigger delegate methods like didSelectRowAtIndexPath: so you have to manually trigger this delegate method

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

或者如果你正在推 ViewControllers 使用segue你必须触发 performSegueWithIdentifier

Or If you are pushing ViewControllers with segue you have to trigger performSegueWithIdentifier

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

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

Now you can push viewController in didSelectRowAtIndexpath.To select the first row in first section or execute whaterver you have written in didSelectRowAtIndexpath: of tableView

对于您的情况,只需按选择第0个索引 didSelectRowAtIndexPath viewController >

As For your case just push the viewController on select at 0th index with 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
}

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

It will show the view controller pushed and if you do not want to animate view controller just pass NO to pushViewController method.

返回按钮,您将返回 viewController

在您的情况下,只需在 viewDidAppear

In your case just write this in your 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)
}

这篇关于以编程方式模拟Swift中UITableViewController中的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆