如何更改事件触发的pickerviews的值

how to change the values of pickerviews triggered by event

我想用 uipickerview 创建我自己的日期选择器,我可以在其中选择 9999 B.C。和 2015 年 A.D。我使用带有多个组件的 UIPickerview。我的前 4 个组件仅包含数字 0-9。 组件 1 包含千年(1000,2000 usw ...)。 我的最后一个组件包含 {A.D。 & B.C.)。根据最后一个选择器,我将更改组件 1 的值,它代表年份的 thousender。如果用户在最后一个组件中选择了 "A.D.",则应该不可能在第一个组件中选择 3-9。

如何实现?

代码:

      func showDatePicker(){
            var datePicker=UIPickerView()
            datePicker.frame=CGRectMake(1, 100, 250, 162)
            datePicker.delegate=self
            datePicker.dataSource = self
            datePicker.tag = 1
            datePicker.backgroundColor=UIColor.orangeColor();
            datePicker.selectRow(180, inComponent: 0, animated: true)
            self.view.addSubview(datePicker)

        }
        func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
            return 5
        }

        func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {


            println("evaluate amount of components");

            if component == 0{
                return 10
            }else if component == 1{
                return 10
            }else if component == 2{
                return 10
            }else if component == 3{
                return 10
            }else if component == 4{
                return 2
            }

            return 10
        }

        func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {

            if component == 0{
                return "\(row)"
            }else if component == 1{
                return "\(row)"
            }else if component == 2{
                return "\(row)"
            }else if component == 3{
                return "\(row)"
            }else if component == 4{
                if(row==0){
                    return "A.D.";
Here I like to limit the values of component 1 to {0,1,2}, because
the year should not go over the year 3000 A.D.
                }else{
                    return "B.C.";

                }
            }else{
                return "?";
            }
        }

** 更新我的解决方案 **

我不知道这是否是最佳做法,但我按如下方式解决了它: 根据年份,我在 pickerview 中显示不同数量的值:

func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {


        println("evaluate amount of components");

        let iYear=DataContainer.sharedInstance.iYear;
        if(iYear > 0){

            if component == 0{
                return 3;
            }else if component == 1{
                return 10;
            }else if component == 2{
                return 10;
            }else if component == 3{
                return 10;
            }else if component == 4{
                return 2;
            }else{
                return 1;
            }

        }else{
            if component == 0{
                return 10
            }else if component == 1{
                return 10
            }else if component == 2{
                return 10
            }else if component == 3{
                return 10
            }else if component == 4{
                return 2
            }else{
                return 1;
            }

        }

    }

当我更改显示 "A.D." 或 "B.C." 的最后一个组件 (5) 时,我只是刷新选择器视图:

func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
...
else if component == 4{
            if(row==0){
                if (iYear < 0 ){
                    //DataContainer.sharedInstance.iYear = -iYear;
                    datePicker.reloadAllComponents();
                }
                return "A.D.";
            }else{
                if (iYear > 0 ){
                    //DataContainer.sharedInstance.iYear = -iYear;
                    datePicker.reloadAllComponents();
                }
                //datePicker.reloadAllComponents();
                return "B.C.";
            }
        }

显然,您必须对第 5 个组件的变化做出反应。在 pickerView(didSelectRow:inComponent) 中处理从或到 A.D. 的更改。也许维护一个标志,指示选择了哪个。

然后调用 reloadComponent()reloadAllComponents 更新选择器视图。