简体   繁体   English

在单元格单击时打开一个 Viewcontroller - Swift

[英]Open a Viewcontroller on cell click - Swift

I am trying to open a new view controller which has a textfield with the text of the cell clicked in a UITableView.我正在尝试打开一个新的视图控制器,它有一个文本字段,其中包含在 UITableView 中单击的单元格文本。 Although I have already discovered how to get the clicked cell's information, I don't know how to push that value to the new ViewController.虽然我已经发现了如何获取被点击单元格的信息,但我不知道如何将该值推送到新的 ViewController。 Here is my current code:这是我当前的代码:

class NearbyViewController: UIViewController, UITableViewDelegate, 
    UITableViewDataSource {

    @IBOutlet weak var nearbyTableView: UITableView!
    var myList: [String] = []


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

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

        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        var (cellName) = myList[indexPath.row]
        cell.textLabel?.text = cellName
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        var (cellName) = myList[indexPath.row]
        cell.textLabel?.text = cellName

        print("row\(indexPath.row)")
        print("name: \(cellName)")

    }

    override func viewDidLoad() {
        super.viewDidLoad()



}

Many Thanks非常感谢

Some playground code that should help you.一些应该对您有所帮助的操场代码。 Call openDetails func with parameters what you need from didSelectRowAt .使用didSelectRowAt您需要的参数调用openDetails func。

import UIKit

final class NearbyViewController: UIViewController {

    // Your controller implementation

    private func openDetails(cellName: String) {
        let detailsController = DetailedInfoViewController() // OR use storyboard.instantiate
        detailsController.cellName = cellName

        // You must have a Navigaion stack for your controller
        self.navigationController?.pushViewController(detailsController, animated: true)
    }
}

final class DetailedInfoViewController: UIViewController {
    var cellName: String!

    // Your controller implementation

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set data into view outlets
    }
}

in your didSelectIndexPath method, you are accessing a cell and setting its label's text.在您的didSelectIndexPath方法中,您正在访问一个单元格并设置其标签的文本。

What you want to do is:你想要做的是:

let viewController = ViewController()
viewController.textFieldText = cell.textLabel.text 
navigationController?.pushViewController(viewController, animated: true)

Then once you have the textFieldText in your ViewController you can say:然后,一旦您在 ViewController 中有textFieldText ,您就可以说:

textField.text = textFieldText

try :尝试:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  //  let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") -> not correct
       let cell = tableView. cellForRowAt(indexpath)  // but don't need cell
    let (cellName) = myList[indexPath.row]
    let viewController = ViewController()
    viewController.textField.Text = cellName 
    self.navigationController?.pushViewController(viewController, animated: true)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift:点击 UICollectionView 的单元格并打开 AlertViewController - Swift: Click onto cell of UICollectionView and open AlertViewController Swift 3 - 关闭当前的 ViewController 并打开新的 ViewController - Swift 3 - Close current ViewController and Open new ViewController Swift - 在点击 TableViewCell 时打开 ViewController - Swift - Open ViewController on Tapping a TableViewCell “LGSideMenucontroller”未在swift 4中的所有viewcontroller中打开 - 'LGSideMenucontroller' not open in all viewcontroller in swift 4 通过单击swift中的TableView单元格转到ViewController - Go to the ViewController by clicking on the TableView cell in swift Swift 4-从UICollectionView单元中推送ViewController - Swift 4 - Push a ViewController from a UICollectionView Cell MONOTOUCH打开一个viewcontroller在tableview单元格上单击 - MONOTOUCH open a viewcontroller on tableview cell is clicked 单击单元格时,ViewController出现两次 - Viewcontroller appear twice when I click cell 如何快速单击弹出式ViewController上的按钮单击导航到TabBar ViewController? - How to navigate to TabBar ViewController on click of button on popover ViewController in swift? 第二个 ViewController 无法正确打开 Swift - Second ViewController won't open properly Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM