在UI Picker中重新加载组件查看Swift

时间:2015-06-14 17:35:36

标签: xcode swift uipickerview uipickerviewcontroller

我无法重新加载基于第一个组件的uPickerView中的第二个组件。我尝试使用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()

}

}

谢谢!

2 个答案:

答案 0 :(得分:0)

这是一个有效的版本。我创建了自己的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()
        }
    }
}

答案 1 :(得分: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!;
}