xcode 通过单击 swift 中的 TableView 单元格转到 ViewController

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

Go to the ViewController by clicking on the TableView cell in swift

iosswiftxcode

提问by aaallleeexxx918

there is a class MenuViewController in which the array recorded in the table is recorded:

有一个MenuViewController类,其中记录了表中记录的数组:

import Foundation
import UIKit

class MenuViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var menuTableView: UITableView!

let myTitle = ["Помощь", "Информация", "Поддержка"]


override func viewDidLoad() {
    super.viewDidLoad()


    menuTableView.delegate = self
    menuTableView.dataSource = self
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return myTitle.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = menuTableView.dequeueReusableCell(withIdentifier: "MenuCell") as! MenuTableViewCell
    cell.labelText.text = myTitle[indexPath.row]
    return cell
}
}

What do you need to write in it to go to Info Controller?

你需要在里面写什么才能进入信息控制器?

采纳答案by Gabriel Pires

I assume you are using storyboards. First you need to set up some sort of connection between your two viewcontrollers. You can do this by setting up a "segue". You hold ctrl+click drag from the first viewcontroller to the 2nd viewcontroller. It looks like this:

我假设您正在使用故事板。首先,您需要在两个视图控制器之间建立某种连接。您可以通过设置“segue”来做到这一点。您按住 ctrl+click 从第一个视图控制器拖动到第二个视图控制器。它看起来像这样:

Creating Segue

创建转场

When you let go of the mouse, you will see this:

当你松开鼠标时,你会看到:

Creating Segue 2

创建转场 2

Click on Show. Now you will have created a segue. Now you need to name it. So click on the segue that shows up in the storyboard. It is basically just an arrow from the Menu View Controller to the Info View Controller. On the right hand side you will see a place where you can name it (give it an identifier):

单击显示。现在您将创建一个 segue。现在你需要给它命名。因此,单击故事板中显示的 segue。它基本上只是一个从菜单视图控制器到信息视图控制器的箭头。在右侧,您会看到一个可以命名的地方(给它一个标识符):

Creating Segue 3

创建转场 3

Now ... you have done all you needed to do in the storyboard. Now in your actual code in the MenuViewController, you need to associate clicking the tableviewcell. You do this by using the delegate method didSelectRowAt.

现在……您已经完成了故事板中需要做的所有工作。现在在 MenuViewController 的实际代码中,您需要关联单击 tableviewcell。您可以使用委托方法 didSelectRowAt 来完成此操作。

I see you have already set up the delegate with this line: menuTableView.delegate = self

我看到你已经用这一行设置了委托: menuTableView.delegate = self

That ensures that didSelectRowAtwill be called whenever the user taps on a row.

这确保了didSelectRowAt每当用户点击一行时都会被调用。

So now what you want to do is write the code to perform the segue:

所以现在你想要做的是编写代码来执行转场:

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

Note: Here is more information on interacting with a tableview row: How to detect tableView cell touched or clicked in swift

注意:这里有更多关于与 tableview 行交互的信息: How to detection tableView cell touched or clicked in swift

Here is more information about segues: IOS - How to segue programmatically using swift

以下是有关转场的更多信息: IOS - 如何使用 swift 以编程方式转场

There are many more customizations you can do ... such as passing data through to the next viewcontroller via the segues. Here's how: Pass data through segue

您可以进行更多自定义……例如通过 segue 将数据传递到下一个视图控制器。方法如下: 通过 segue 传递数据

回答by Jeetendra Kumar

implement UITableViewDelegate method:

实现 UITableViewDelegate 方法:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

       //To InfoViewController I am considering you have created InfoViewController with XIB 
       let controller = InfoViewController(nibName: "YourNibName", bundle: nil)

        //Present Your Controller
        self.present(controller, animated: true) {
        }              

        //Push Your controller if your view is already part of NavigationController stack
        self.navigationController?.pushViewController(controller, animated: true)
    }

回答by Gowtham Sooryaraj

Just add a
nextViewController.didMove(toParentViewController: self) inside a "didSelectRowAt" example given below

只需在
下面给出的“didSelectRowAt”示例中添加一个nextViewController.didMove(toParentViewController: self)

this is for List a tableview cell

这是用于列出 tableview 单元格

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cellIdentifier = "cell"
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! PatientFamilyDetailTableViewCell
        cell.txt_patientName.text = patientname[indexPath.row]

        // Configure the cell...

        return cell
    }

Now it's for Select a Table view cell

现在它用于选择表视图单元格

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let cell = tableView.cellForRow(at: indexPath) {
            if cell.isSelected {


                let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
                let nextViewController = storyBoard.instantiateViewController(withIdentifier: "medtestmain") as! medTest
            patient_id = relation[indexPath.row]
            patient_name = patientname[indexPath.row]
            UserDefaults.standard.set(patient_id, forKey: "patient_id")
            UserDefaults.standard.set(patient_name, forKey: "patient_name")
                self.addChildViewController(nextViewController)
                nextViewController.view.frame = self.view.frame
                self.view.addSubview(nextViewController.view)
                nextViewController.didMove(toParentViewController: self)

            }
        }
    }

Hope it's help

希望有帮助