gpt4 book ai didi

swift - 如何用字典填充选择器 View ?

转载 作者:搜寻专家 更新时间:2023-11-01 06:51:36 24 4
gpt4 key购买 nike

我想用字典填充选择器,但在设置行标题时遇到了问题。

基本上我的选择器应该有 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? {

//

}

我可以用两个选择器来解决这个问题,但我想知道如何用两个组件和字典来解决这个问题。我基本上想学习如何使用字典填充选择器。

最佳答案

您最好创建一个结构来保存各州及其邮政编码。字典很快就会变得困惑。无论如何,这是代码。请参阅其中的评论。

class ViewController: UIViewController {
@IBOutlet weak var pickerView: UIPickerView!
var stateToPostalCodeDB = [
"Alabama": ["35010", "35011","35012"],
"Arizona": ["99501", "99502"],
"California": ["90001", "90002", "90003"]
]

/// The keys in a dictionary are unordered
/// Create a computed property that sorts the states alphabetically
var states: [String] { return stateToPostalCodeDB.keys.sorted() }

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

/// An enum to define the columns of the UIPickerView
/// Better than using 0 or 1 or any mysterious number
private enum PickerViewComponents: Int, CaseIterable {
case state, zipCode
}

/// The data source and delegate of the Picker View
extension ViewController: UIPickerViewDataSource, UIPickerViewDelegate {
/// The number of columns of the Picker View
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return PickerViewComponents.allCases.count
}

/// The number of rows for each columns
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let componentName = PickerViewComponents.allCases[component]
switch componentName {
case .state:
return states.count
case .zipCode:
let index = pickerView.selectedRow(inComponent: PickerViewComponents.state.rawValue)
let state = states[index]
return stateToPostalCodeDB[state]!.count
}
}

/// The label for each row
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let componentName = PickerViewComponents.allCases[component]
switch componentName {
case .state:
return states[row]
case .zipCode:
let index = pickerView.selectedRow(inComponent: PickerViewComponents.state.rawValue)
let state = states[index]
return stateToPostalCodeDB[state]?[row]
}
}

/// If the user changes the state, reload the zip codes
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let componentName = PickerViewComponents.allCases[component]
if componentName == .state {
let zipCode = PickerViewComponents.zipCode.rawValue
pickerView.reloadComponent(zipCode)
pickerView.selectRow(0, inComponent: zipCode, animated: true)
}
}
}

关于swift - 如何用字典填充选择器 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56859315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com