2012-09-21 24 views
0

使用objC,似乎有一個名爲numberOfComponentsInPickerView的方法,您可以使用該方法更改UIPickerView中組件的數量。一旦這用於更改組件數量,您將重新加載組件以「刷新」Picker。即[picker reloadAllComponents];如何使用MonoTouch更改UIPickerView中組件(列)的數量?

我的問題是我將如何使用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; 

從本質上講,我只有當在第一個組件列中選擇了「Custom」的值時,才允許用戶從該組件中選擇一個值,只要在第一個組件中選擇了「Custom」 。這可能嗎?我知道如何通過用「GetComponentCount」返回一個不同的數字來改變組件的數量,它只是在已經創建了選擇器的時候隨時改變它,我不確定。

感謝您的幫助!

回答

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的成員。

+0

嗨,喬恩,謝謝 - 這正是我以前的樣子。我現在看到該布爾值已設置,然後調用重載 - 此時GetComponentCount中的if語句將被計算,如果布爾值爲true,則返回3!非常感謝 :) – Shogan