【问题标题】:Is it possible to give UITableViewCells each a unique ViewController?是否可以为每个 UITableViewCells 提供一个唯一的 ViewController?
【发布时间】: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


【解决方案1】:

我建议创建一个枚举来确认 CaseIterable。同时实现func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)。

class ViewController: UITableViewController {
    
    enum OptionTypes: CaseIterable {
        case ruleOf72
        case futureValue
        case simpleInterest
        
        var description: String {
            switch self {
            case .ruleOf72: return "Rule of 72"
            case .futureValue: return "Future Value"
            case .simpleInterest: return "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)
        let item = OptionTypes.allCases[indexPath.row]
        cell.textLabel?.text = item.description
        return cell
    }
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return OptionTypes.allCases.count
    }
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let item = OptionTypes.allCases[indexPath.row]
        switch item {
        case .ruleOf72:
            navigateToRuleOf72ViewController()
        case .futureValue:
            navigateToFutureValueViewController()
        case .simpleInterest:
            navigateToSimpleInterestViewController()
        }
    }
    
    
    private func navigateToRuleOf72ViewController() {}
    
    private func navigateToFutureValueViewController() {}

    private func navigateToSimpleInterestViewController() {}

}

【讨论】:

    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2015-02-28
    • 2020-06-13
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多