简体   繁体   中英

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. 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. 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 .

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.

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:

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)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM