在单元格单击时打开一个 Viewcontroller - Swift [英] Open a Viewcontroller on cell click - Swift

查看:16
本文介绍了在单元格单击时打开一个 Viewcontroller - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开一个新的视图控制器,它有一个文本字段,其中包含在 UITableView 中单击的单元格文本.虽然我已经发现了如何获取被点击单元格的信息,但我不知道如何将该值推送到新的 ViewController.这是我当前的代码:

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



}

非常感谢

推荐答案

一些应该对您有所帮助的 Playground 代码.使用 didSelectRowAt 中的参数调用 openDetails func.

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

这篇关于在单元格单击时打开一个 Viewcontroller - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆