0

使用 objC,似乎有一个名为 numberOfComponentsInPickerView 的方法,您可以使用它来更改 UIPickerView 中的组件数量。一旦这用于更改组件的数量,您将重新加载组件以“刷新”选取器。即[picker reloadAllComponents];

我的问题是如何使用 MonoTouch 做到这一点?我是 Mono 的新手,在搜索了我的选择器对象上的所有方法和属性之后,我似乎找不到这个方法。

这是我创建 UIPickerView 的方式:

public class PeopleModel : UIPickerViewModel {
        static string [] names = new string [] {
            "item1",
            "item2",
            "item3",
            "item4",
            "item5",
            "Custom"
        };

        ActionSheetPickerExampleViewController pvc;
        public PeopleModel (ActionSheetPickerExampleViewController pvc) {
            this.pvc = pvc;
        }

        public override int GetComponentCount (UIPickerView v)
        {
            return 2;
        }

        public override int GetRowsInComponent (UIPickerView pickerView, int component)
        {
            if (component == 0) {
                return names.Length;
            } else {
                return 50;
            }
        }

        public override string GetTitle (UIPickerView picker, int row, int component)
        {
            if (component == 0)
                return names [row];
            else
                return row.ToString ();
        }

        public override void Selected (UIPickerView picker, int row, int component)
        {
            pvc.diskLabel.Text = String.Format ("{0} - {1}",
                                            names [picker.SelectedRowInComponent (0)],
                                            picker.SelectedRowInComponent (1));
            Console.WriteLine (names[picker.SelectedRowInComponent(0)]);
            if (names [picker.SelectedRowInComponent(0)] == "Custom") {
                Console.WriteLine ("Custom selected!");
                picker.ReloadComponent(1);
                using(var alert = new UIAlertView("Select value", "Select custom disk speed",null,"OK","Button2"))
                {
                    alert.Show ();
                }

            }
        }

        public override float GetComponentWidth (UIPickerView picker, int component)
        {
            if (component == 0)
                return 240f;
            else
                return 40f;
        }

        public override float GetRowHeight (UIPickerView picker, int component)
        {
            return 40f;
        }
    }

所以上面是类,然后我在这样的操作表上创建选择器:

actionSheetPicker = new ActionSheetPicker (this.View);
        actionSheetPicker.Title = "Choose disk speed:";
        actionSheetPicker.Picker.Model = new PeopleModel (this);
        actionSheetPicker.Picker.Hidden = false;

本质上,我正在考虑向此 Picker 添加另一个组件,仅当在第一个组件列中选择“自定义”的值时,然后只要在“自定义”中选择了“自定义”,就允许用户从该组件中选择一个值第一个组件。这可能吗?我知道如何在创建选择器之前通过使用“GetComponentCount”返回一个不同的数字来更改组件的数量,它只是在选择器已经创建后即时更改,我不确定。

感谢您的帮助!

4

1 回答 1

2

我对您的 PeopleModel 进行了一些更改,以跟踪是否选择了自定义,以及是否显示第三个组件:

public class PeopleModel : UIPickerViewModel
    {
        bool _custom;
        static string[] names = new string [] {
            "item1",
            "item2",
            "item3",
            "item4",
            "item5",
            "Custom"
        };

        ActionSheetPickerExampleViewController pvc;
        public PeopleModel (ActionSheetPickerExampleViewController pvc) {
            this.pvc = pvc;
        }

        public override int GetComponentCount (UIPickerView v) {
            if (_custom)
                return 3;
            return 2;
        }

        public override int GetRowsInComponent (UIPickerView pickerView, int component) {
            if (component == 0) {
                return names.Length;
            } else if (component == 1) {
                return 50;
            } else
                return 3;
        }

        public override string GetTitle (UIPickerView picker, int row, int component) {
            if (component == 0)
                return names [row];
            else if (component == 1)
                return row.ToString ();
            else
                return row.ToString ();
        }

        public override void Selected (UIPickerView picker, int row, int component) {
            pvc.diskLabel.Text = String.Format ("{0} - {1}",
                                                names [picker.SelectedRowInComponent (0)],
                                                picker.SelectedRowInComponent (1));
            Console.WriteLine (names [picker.SelectedRowInComponent (0)]);
            _custom = names [picker.SelectedRowInComponent (0)] == "Custom";
            picker.ReloadAllComponents ();

        }

        public override float GetComponentWidth (UIPickerView picker, int component) {
            if (component == 0)
                return 240f;
            else
                return 40f;
        }

        public override float GetRowHeight (UIPickerView picker, int component) {
            return 40f;
        }
    }

我认为这就是你想要完成的。方法 ReloadAllComponents() 是 UIPickerView 的成员。

于 2012-09-21T23:27:51.550 回答