以编程方式从一个 ViewController 导航到 didSelectRowAt 上的另一个 [英] Navigate from a ViewController to an another on didSelectRowAt programmatically

查看:14
本文介绍了以编程方式从一个 ViewController 导航到 didSelectRowAt 上的另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含自定义 UITableViewUIViewController.该表也有一个自定义 UITableViewCell.

I have a UIViewController that contains a custom UITableView. The table has a custom UITableViewCell too.

当您选择/单击其中一行时,如何从第一个 ViewController 导航到另一个?

How to navigate from the first ViewController to an another when you select/click one of the rows?

我没有使用过 StoryBoard.

这是我的代码.每个类都是外部文件.如果您需要更多代码,请告诉我.

This my code. Each one of the classes are external file. Let me know, if you need more code.

class TestViewController: UIViewController, UITableViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        view.addSubview(testTableView)
    }

    let testTableView: TestTableView = {
        let table = TestTableView()
        table.register(TestTableViewCell.self, forCellReuseIdentifier: TestTableViewCell.identifier)
        return table
    }()
}

class TestTableView: UITableView,  UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
       let cell = tableView.dequeueReusableCell(withIdentifier: TestTableViewCell.identifier, for: indexPath)
        return cell
    }
}



class TestTableViewCell: UITableViewCell {
    static let identifier  = "testCell"
}

推荐答案

这里有一个完整的答案:

Here is a complete answer:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let newViewController = NewViewController()
    self.navigationController?.pushViewController(newViewController, animated: true)
}

这篇关于以编程方式从一个 ViewController 导航到 didSelectRowAt 上的另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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