在 UI Picker View Swift 中重新加载组件

Posted

技术标签:

【中文标题】在 UI Picker View Swift 中重新加载组件【英文标题】:Reload Component in UI Picker View Swift 【发布时间】:2015-06-14 17:35:35 【问题描述】:

我无法在我的UIPickerView 中重新加载基于第一个组件的第二个组件。我尝试使用reloadAllComponents,但它要么不起作用要么崩溃。我正在尝试将汽车制造商放在左边,将他们的汽车型号放在右边。因此,当汽车进行更改时,它会同时更改正确的组件模型。

class RegistrationPage: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate 
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var passwordReTypeTextField: UITextField!
    @IBOutlet weak var makePicker: UIPickerView!
    @IBOutlet weak var modelPicker: UIPickerView!
    @IBOutlet weak var licenseTagTextField: UITextField!
    var picker1Options = []
    var picker2Options = []

    @IBAction func SubmitButton(sender: AnyObject) 

    

    override func viewDidLoad() 
        super.viewDidLoad()

        makePicker = UIPickerView()
        modelPicker = UIPickerView()
        let makeAndModel = CarMakeModel();

        picker1Options = makeAndModel.makeValues()
        let firstValue = picker1Options.objectAtIndex(0)
        picker2Options = makeAndModel.modelValues(firstValue as! String)

    

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 
        return 2
    

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        if (component == 0) 
            return picker1Options.count
         else 
            return picker2Options.count
        

    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 
        if component == 0 
            return "\(picker1Options[row])"
         else 
            let makeAndModel = CarMakeModel();
            let currentValue = picker1Options.objectAtIndex(row)
            var picker2ptions = makeAndModel.modelValues(currentValue as! String)
            return "\(picker2ptions[row])"
        
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        let makeAndModel = CarMakeModel();
        let currentValue = picker1Options.objectAtIndex(row)
        var picker2ptions = makeAndModel.modelValues(currentValue as! String)
        pickerView.reloadAllComponents()

    


谢谢!

【问题讨论】:

【参考方案1】:

这是一个有效的版本。我创建了自己的CarMakeModel 类来测试它。您过度考虑了 titleForRow 函数,这是您的主要问题。此外,在didSelectRow 中,您需要更新类属性picker2Options 而不是局部变量。我改变了其他的东西。我建议您将我的版本与您的版本逐行比较,以查看差异。

另外,只需要 1 个 pickerView,它应该连接到 Storyboard 中的那个。你不应该实例化你自己的pickerViews。

class CarMakeModel 
    func makeValues() -> [String] 
        return ["Toyota", "Honda"]
    

    func modelValues(make: String) -> [String] 
        if make == "Toyota" 
            return ["Camry", "Sienna", "Highlander"]
         else 
            return ["Accord", "Civic", "Pilot"]
        
    


class RegistrationPage: UIViewController,UIPickerViewDataSource, UIPickerViewDelegate 
    @IBOutlet weak var usernameTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    @IBOutlet weak var passwordReTypeTextField: UITextField!
    @IBOutlet weak var makePicker: UIPickerView!
    @IBOutlet weak var modelPicker: UIPickerView!
    @IBOutlet weak var licenseTagTextField: UITextField!
    var picker1Options:[String] = []
    var picker2Options:[String] = []

    @IBAction func SubmitButton(sender: AnyObject) 

    

    override func viewDidLoad() 
        super.viewDidLoad()

        //makePicker = UIPickerView()
        //modelPicker = UIPickerView()
        let makeAndModel = CarMakeModel();

        picker1Options = makeAndModel.makeValues()
        let firstValue = picker1Options[0]
        picker2Options = makeAndModel.modelValues(firstValue)

    

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 
        return 2
    

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
        if (component == 0) 
            return picker1Options.count
         else 
            return picker2Options.count
        

    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 
        if component == 0 
            return "\(picker1Options[row])"
         else 
            return "\(picker2Options[row])"
        
    

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) 
        if component == 0 
            let makeAndModel = CarMakeModel();
            let currentValue = picker1Options[row]
            picker2Options = makeAndModel.modelValues(currentValue)
            pickerView.reloadAllComponents()
        
    

【讨论】:

如果你是这个解决方案,你将如何修改字体大小? @vacawama【参考方案2】:

字体大小修改:

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView 

    var pickerLabel = view as? UILabel;

    if (pickerLabel == nil)
    
        pickerLabel = UILabel()

        pickerLabel?.font = UIFont(name: "Helvetica Neue", size: 10)
        pickerLabel?.textAlignment = NSTextAlignment.center
    

    if component == 0 
        pickerLabel?.text = picker1Options[row]
     else 
        pickerLabel?.text = picker2Options[row]
    

    return pickerLabel!;

【讨论】:

以上是关于在 UI Picker View Swift 中重新加载组件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift UI 中删除 Picker 视图的灰色背景框

iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

iOS开发UI篇—使用picker View控件完成一个简单的选餐应用

Picker View,如何返回选中项的索引。斯威夫特 4

在 swift ui 中找不到 PopUpButtonPickerStyle [关闭]

类型“ContentView”不符合协议“View”(Xcode - Swift UI)