xcode 导航到表格视图中每个单元格的新视图控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42195290/
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-09-15 09:47:12  来源:igfitidea点击:

Navigate to a new view controller for every cell in table view

swiftxcode

提问by Nouman

I am a beginner coder in swift. I am trying to create a tabbed application. For one of my tabs, I am creating a table view which has multiple rows each which have a different task (A good way to think of this is the facebook app, where each option in the more screen will take you to a separate view)

我是 swift 的初学者编码员。我正在尝试创建一个选项卡式应用程序。对于我的一个选项卡,我正在创建一个表视图,其中有多行,每行都有不同的任务(考虑这个的一个好方法是 facebook 应用程序,其中更多屏幕中的每个选项都会带你到一个单独的视图)

Now, my table is populated with an array:

现在,我的表中填充了一个数组:

let array  = ["one", "two", "three]

I want to ask, that everytime that I tap on one of these rows, I would like to go a new view controller. How is this possible?

我想问,每次我点击这些行之一时,我想要一个新的视图控制器。这怎么可能?

What I tried was performSeguewith an identifierwhich I give in the storyboard, but then there would be an xamount of segues connecting to the table view? So I don't think this is right? :/

我尝试了performSegueidentifier了我所赐的故事板,但随后会有一个x连接到表视图塞格斯的金额是多少?所以我认为这是不对的?:/

I know the contents of the array prior to generating the table, so If I know the array value, and the row being tapped, how can I navigate to a new view controller?

我在生成表之前知道数组的内容,所以如果我知道数组值和被点击的行,我如何导航到新的视图控制器?

Edit:

编辑:

When performing the segue between the controllers, I am using:

在控制器之间执行 segue 时,我使用:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.performSegue(withIdentifier: "showView", sender: self)
}

This of course will only connect to the segue showViewhowever, how can i add multiple view controllers?

这当然只会连接到 segueshowView但是,我如何添加多个视图控制器?

回答by Nirav D

You need to simply compare which row is selected in tableViewand then perform segue according to it.

您只需比较选择了哪一行tableView,然后根据它执行segue。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let segueIdentifier: String
    switch indexPath.row {
    case 0: //For "one"
         segueIdentifier = "showView1"
    case 1: //For "two"
         segueIdentifier = "showView2"
    default: //For "three"
         segueIdentifier = "showView3"
    }
    self.performSegue(withIdentifier: segueIdentifier, sender: self)
}

回答by javimuu

Add the following function to your controller.

将以下功能添加到您的控制器。

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    navigationController?.pushViewController(NewController(), animated: true)
}

回答by Mark Moeykens

Using Static Cells in a UITableViewController

在 UITableViewController 中使用静态单元格

Summary

概括

An alternative is to use a UITableViewController with static cells. If you know already know the menu then you can just create static cells for each item.

另一种方法是使用带有静态单元格的 UITableViewController。如果您已经知道菜单,那么您可以为每个项目创建静态单元格。

In the storyboard you can connect a segue from each cell to their respective view controllers.

在故事板中,您可以将每个单元格的 segue 连接到它们各自的视图控制器。

Example

例子

example

例子