如何用字典填充选择器视图?

时间:2019-07-02 20:01:36

标签: swift

我想用词典填充选择器,而行标题出现问题。

基本上,我的选择器应包含2个组件,如下所示:

|Alabama| ---> ["35010", "35011","35012"]
|Arizona| ---> ["99501", "99502"]
|California|--> ["90001", "90002", "90003"]

第一个选择器应具有状态,并且根据状态,第二个选择器应具有不同的值。 我已经创建了一个字典,将组件的数量设置为两个,状态为键,代码作为组件值,但是我无法在选择器中填充值。

 class dependentPicker: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {



     @IBOutlet weak var dependentPicker: UIPickerView!

        let stateToPostalCodeDB = [
            "Alabama" :[ "35010","35011","35012"]
            "Arizona" : [ "85001", "85002"],
            "California": [ "90001","90002","90003"]]

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 2
        }
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            //
            }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

            //

        }

我可以用两个选择器来解决问题,但是我想知道如何使用2个组件和字典来做同样的事情。我基本上想学习如何用字典填充选择器。

2 个答案:

答案 0 :(得分:1)

最好创建一个结构来保存状态及其邮政编码。字典会很快使人迷惑。无论如何,这是代码。查看其中的评论。

192.168.10.10

答案 1 :(得分:0)

此答案与CodeDifferent的出色答案非常相似,但有两个主要区别:

  1. State结构组成的数组替换了字典。
  2. 硬编码的值替换了PickerViewComponents枚举。我认为枚举会增加不必要的冗长性和复杂性。

这是主要的视图控制器类:

class ViewController: UIViewController {
    @IBOutlet weak var pickerView: UIPickerView!

    let states = [
        State(name: "Alabama", postalCodes: ["35010", "35011","35012"]),
        State(name: "Arizona", postalCodes: ["99501", "99502"]),
        State(name: "California", postalCodes: ["90001", "90002", "90003"])
    ]

    override func viewDidLoad() {
        pickerView.dataSource = self
        pickerView.delegate = self
    }
}

数据源和委托:

extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate {
    /// Number of columns
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 2
    }

    /// Number of rows per column
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == Component.state {
            return states.count
        } else {
            let index = pickerView.selectedRow(inComponent: Component.state)
            return states[index].count
        }
    }

    /// Title for row/column
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component == Component.state {
            return states[row]
        } else {
            let index = pickerView.selectedRow(inComponent: Component.state)
            return states[index].postalCodes[row]
        }
    }

    /// Update zip codes if state changes
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if component == Component.state {
            pickerView.reloadComponent(Component.postalCode)
            pickerView.selectRow(0, inComponent: Component.postalCode, animated: true)
        }
    }
}

结构:

struct State {
    var name: String
    var postalCodes: [String]
}

struct Component {
    static let state = 0
    static let postalCode = 1
}