首页 > 解决方案 > 再次调用同一个视图,以便可以在 UITableViewController 中选择另一个选项

问题描述

背景: 简单的应用程序,可让您从 a 中选择货币UITableViewController,再次调用同一视图以进行第二选择,然后将用户带到显示两种选定货币和汇率的新视图

所以理论上对我来说,这只是2个观点。第一个是货币列表,第二个是显示所选货币/汇率。第一个视图是完整的设计明智的。但是我正在努力解决如何在第一个和第二个选择之间建立联系,因为它调用了相同的视图。我该怎么做?

在我的didSelectRowAt中,我通常会执行Segue,但是如何调用同一个视图并记录从第一个视图中选择的值?我的一个想法是调用一个函数来记录是否选择了一个选项,如果是这样,调用新视图,否则再次调用相同的视图,但我不确定我将如何实现它。任何帮助表示赞赏!

到目前为止我的代码:

import UIKit

class SelectCurrencyTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    // Get the JSON data to insert into the table
    func parseJSONData()-> Array<Any> {
        var finalArray = [Any]()
        if let url = Bundle.main.url(forResource: "currencies", withExtension: "json") {
            do {
                let data = try Data(contentsOf: url)
                let jsonResult = try JSONSerialization.jsonObject(with: data)
                if var jsonArray = jsonResult as? [String] {

                    while jsonArray.count > 0 {
                        let result: [String] = Array(jsonArray.prefix(2))
                        finalArray.append(result)
                        jsonArray.removeFirst(2)
                    }
                }
            } catch {
                print(error)
            }
        }
        return finalArray
    }

    func checkOptionsCount()-> Int{
        // somehow check if option selected?
        return 1 
    }

    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return parseJSONData().count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCellController
        if let array = parseJSONData()[indexPath.row] as? [String]{
            cell.countryCodeLabel.text = array[0]
            cell.currencyLabel.text = array[1]
            cell.countryFlag.image = UIImage(named: array[0])
        }
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        // if this is 1st time, present view again
        if (checkOptionsCount() == 1){



        // if this is 2nd time, show new view
        } else if (checkOptionsCount() == 2){
             // performSegue with new view 


        } else {
            print("How did I get here")
        }
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

标签: swiftuitableview

解决方案


看到您的代码,我假设您正在使用情节提要。完成您想要的一种方法可能是这样的:

  1. 在 Interface Builder 中选择您的 SelectCurrencyTableViewController 并向其添加 Storyboard ID:在此处输入图像描述
  2. 添加一个属性,您将在其中存储您选择的货币到 SelectCurrencyTableViewController,如下所示:

    class SelectCurrencyTableViewController: UITableViewController {
    
        var selectedCurrency: Currency?
        //...
    }
    
  3. 然后在 didSelectRow 中:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    
    
        // if this is 2nd time, show new view
        if let selected = selectedCurrency {
    
            // performSegue with new view 
    
        // if this is 1st time, present view again
        // these is no selected currency passed from previous view controller, so this is the first time
        } else {
    
            //get view controller from storyboard using storyboard id (replace "Main" with your storyboard's name
            let vc = UIStoryboard(name: "Main", bundle: nil)
                .instantiateViewController(withIdentifier: "SelectCurrencyTableViewController") as! SelectCurrencyTableViewController
            vc.selectedCurrency = //place code for getting first currency based on indexPath.row here
            show(vc, sender: self)
        }
    }
    

推荐阅读