使用objC,似乎有一个名为numberOfComponentsInPickerView的方法,您可以使用它来更改UIPickerView中的组件数。一旦这用于更改组件的数量,您将重新加载组件以“刷新”选择器。即[picker reloadAllComponents];
我的问题是如何使用MonoTouch执行此操作?我是Mono的新手,在搜索了我的picker对象上的所有方法和属性之后,我似乎无法找到这种方法。
以下是我创建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添加另一个组件,只有在第一个组件列中选择“Custom”的值时,然后允许用户从“Custom”中选择一个值,只要“Custom”在第一个组件中选择。这可能吗?我知道如何在创建选择器之前通过使用“GetComponentCount”返回不同的数字来更改组件数量,只需在创建选择器时即时更改它,我不确定。
感谢您的帮助!
答案 0 :(得分: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的成员。