以编程方式在 didSelectRowAt 上从一个 ViewController 导航到另一个

编程入门 行业动态 更新时间:2024-10-27 13:27:46
本文介绍了以编程方式在 didSelectRowAt 上从一个 ViewController 导航到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个包含自定义 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)
}

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

更多推荐

[db:关键词]

本文发布于:2023-04-29 02:57:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1183575.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   didSelectRowAt   ViewController

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!