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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24787098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:25:12  来源:igfitidea点击:

Programmatically emulate the selection in UITableViewController in Swift

iosuitableviewswift

提问by Alexey Nakhimov

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

很抱歉没有任何代码行的问题。如果可能的话,我需要如何进行的建议。

In Swift language.

使用 Swift 语言。

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

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

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

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

Once again I apologize and thank you in advance!

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

回答by codester

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

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

In Swift

在斯威夫特

    let rowToSelect:NSIndexPath = NSIndexPath(forRow: 0, inSection: 0);  //slecting 0th row with 0th section
    self.tableView.selectRowAtIndexPath(rowToSelect, animated: true, scrollPosition: UITableViewScrollPosition.None);

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

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

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

Or If you are pushing ViewControllerswith segue you have to trigger performSegueWithIdentifier

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

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

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

现在您可以将 viewController 推入 .TodidSelectRowAtIndexpath选择第一部分中的第一行或执行您编写 didSelectRowAtIndexpath:的任何内容tableView

As For your case just push the viewControlleron select at 0th index with didSelectRowAtIndexPath

至于你的情况,只需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
}

It will show the view controller pushed and if you do not want to animate view controller just pass NOto pushViewControllermethod.

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

On pressing Backbutton you will back to your viewController

按下Back按钮后,您将返回到您的viewController

In your case just write this in your viewDidAppear

在你的情况下,只需在你的 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)
}

回答by John Carto

You can also do this simply by accessing the tableView Delegate to force "didSelectRowAtIndex" to fire

您也可以通过访问 tableView Delegate 强制“didSelectRowAtIndex”触发来完​​成此操作

//programatically select a row to work with

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

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

// Programatically selecting row does not fire the "didSelectRowAtIndexPath" function so we must manually force the firing as per below

// 以编程方式选择行不会触发“didSelectRowAtIndexPath”函数,因此我们必须按照以下方式手动强制触发

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