【发布时间】:2021-11-24 05:16:30
【问题描述】:
为了扩展,我有一个包含两个单元格的 UITableView,我想在点击时为每个单元格提供它们自己独特的视图控制器(因为它们的功能会有所不同)。我已经搜索了这个网站以及谷歌,但找不到答案。这是我的 UITableView 的设置:
class ViewController: UITableViewController {
let functions = ["Rule of 72","Future Value", "Simple Interest"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.functions[indexPath.row]
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 3
}
}
最终,我们的想法是构建一个允许计算某些金融和经济方程式的应用程序。这些方程需要不同的输入和输出(72 规则只需要一个 UITextField,而 Future Value 需要四个 UITextField)。
【问题讨论】:
-
添加函数 tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 并在点击单元格时使用它来呈现不同的视图控制器
-
showSegue 你的 ViewController 在 UiTableView 中,didSelectRowAt indexPath
标签: ios swift uitableview viewcontroller