1

我无法重新加载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()

    }
}

谢谢!

4

2 回答 2

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()
        }
    }
}
于 2015-06-14T18:58:57.047 回答
0

对于字体大小修改:

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!;
}
于 2017-02-18T02:55:25.587 回答