gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 05:32:26 26 4
gpt4 key购买 nike

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

使用 Swift 语言。

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

有没有其他办法?当应用程序启动时,以编程方式模拟表中第一行的选择,并立即显示 UIViewController 以及有关所选值的详细信息。从这个 Controller 可以通过 NavigationController 返回到 UITableViewController

再次向您致歉并提前致谢!

最佳答案

是的,你可以快速做到这一点。只需像往常一样加载你的 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: 这样的委托(delegate)方法,所以你必须手动触发这个委托(delegate)方法

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

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

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

现在您可以在 didSelectRowAtIndexpath 中推送 viewController。要选择第一部分中的第一行或执行您在 didSelectRowAtIndexpath: 中编写的任何内容 tableView

对于您的情况,只需使用 didSelectRowAtIndexPath 在第 0 个索引处选择 viewController 即可

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
}

它将显示推送的 View Controller ,如果您不想为 View Controller 设置动画,只需将 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)
}

关于ios - 以编程方式模拟 Swift 中 UITableViewController 中的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24787098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com