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

2023-06-10移动开发问题
1

本文介绍了以编程方式从一个 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 上的另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在 cocos2d 中添加 UIViewController
Add UIViewController in cocos2d(在 cocos2d 中添加 UIViewController)...
2024-08-12 移动开发问题
4

UIScrollView 和 Cocos2D
UIScrollView and Cocos2D(UIScrollView 和 Cocos2D)...
2024-08-12 移动开发问题
2

对 <GKModalRootViewController: 0xb7e450> 的开始/结束
Unbalanced calls to begin/end appearance transitions for lt;GKModalRootViewController: 0xb7e450gt;(对 lt;GKModalRootViewController: 0xb7e450gt; 的开始/结束外观转换的不平衡调用)...
2024-08-11 移动开发问题
5

无法将文件从捆绑包复制到 iOS 中的文档目录
Can`t copy file from bundle to documents directory in iOS(无法将文件从捆绑包复制到 iOS 中的文档目录)...
2024-04-15 移动开发问题
4

如何复制“字典"在斯威夫特?
How to copy a quot;Dictionaryquot; in Swift?(如何复制“字典在斯威夫特?)...
2024-04-15 移动开发问题
10

Swift - 迭代结构对象时如何对其进行变异
Swift - How to mutate a struct object when iterating over it(Swift - 迭代结构对象时如何对其进行变异)...
2024-04-15 移动开发问题
7