以编程方式从 ViewController 导航到 didSelectRowAt 上的另一个
Navigate from a ViewController to an another on didSelectRowAt programmatically
我有一个包含自定义 UITableView
的 UIViewController
。 table 也有自定义 UITableViewCell
。
当您 select/click 其中一行时,如何从第一个 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"
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = yourCustomVCName (nibName: "yourCustomVC nibName" , bundle : nil)
self.present(vc, animated: true , completion: nil)
}
完整答案如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
在 UIKit
中,为了以编程方式从 TableCell
导航,您可以在没有故事板的情况下完成。
您有两种查看方式 viewController
- 如果要赠送......
动画从下到上
(要记住的一件事)你不能在不关闭之前显示的屏幕的情况下显示新视图,否则由于显示的屏幕冲突导致应用程序崩溃
yourTableView.deselectRow(at: indexPath, animated: true)//used for single Tap
let vC = YourViewController (nibName: "" , bundle : nil)
vC.modalPresentationStyle = .fullScreen //this line is optional for fullscreen
self.present(vC, animated: true , completion: nil)
- 或者如果你想正常查看你的viewController(2是击球手)....
动画从右到左
yourTableView.deselectRow(at: indexPath, animated: true)
let vC = YourViewController()
self.navigationController?.pushViewController(vC, animated: true)
我有一个包含自定义 UITableView
的 UIViewController
。 table 也有自定义 UITableViewCell
。
当您 select/click 其中一行时,如何从第一个 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"
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = yourCustomVCName (nibName: "yourCustomVC nibName" , bundle : nil)
self.present(vc, animated: true , completion: nil)
}
完整答案如下:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
}
在 UIKit
中,为了以编程方式从 TableCell
导航,您可以在没有故事板的情况下完成。
您有两种查看方式 viewController
- 如果要赠送......
动画从下到上 (要记住的一件事)你不能在不关闭之前显示的屏幕的情况下显示新视图,否则由于显示的屏幕冲突导致应用程序崩溃
yourTableView.deselectRow(at: indexPath, animated: true)//used for single Tap
let vC = YourViewController (nibName: "" , bundle : nil)
vC.modalPresentationStyle = .fullScreen //this line is optional for fullscreen
self.present(vC, animated: true , completion: nil)
- 或者如果你想正常查看你的viewController(2是击球手)....
动画从右到左
yourTableView.deselectRow(at: indexPath, animated: true)
let vC = YourViewController()
self.navigationController?.pushViewController(vC, animated: true)