我有一个包含自定义 UITableView
的 UIViewController
。该表也有一个自定义的 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/