gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 14:05:32 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com