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

标签 swift uitableview uiviewcontroller uinavigationcontroller

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

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

注意事项

我没有使用过StoryBoard

更新

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

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

最佳答案

这是一个完整的答案:

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

关于swift - 以编程方式在 didSelectRowAt 上从一个 ViewController 导航到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53020856/

相关文章:

ios - UITableViewController 作为 subview 获取错误

插入 View Controller 时快速背景颜色

ios - 环球银行金融电信协会 : UITableView passing all cell's data(TextField & Label) to UIViewController

iphone - 如何在其父 View 之上添加 subview 中的 View ?

ios - UICollectionView 粘性补充 View

ios - SplitViewController 显示 iPad Pro 纵向模式的细节

iphone - 在 Objective C 中解析二维数组

iphone - 文字转语音:阅读列表并在标题之间播放音频文件

ios - pList IOS Swift 中的数据是无序的

ios - 使用大的导航标题和适合的文本