2011-06-06 73 views
3

我在一個項目中,我必須執行以下兩項工作的工作填充UIPickerView: 1)從CoreData取值,並將其存儲在一個的NSMutableArray。 2.)以UIPickerView並填充一個數組值。帶有一個數組值

問題是陣列的大小是動態的,我很喜歡填充數組值UIPickerView。有人能幫我嗎。

+0

什麼是'UIPickercontroller'? – Jhaliya 2011-06-06 09:57:19

+0

你到現在爲止嘗試過什麼?你正在使用UIPickerView?你是否爲它實現了委託方法? – 2011-06-06 10:02:28

+0

@jhaliya UIPickercontroller用於顯示數組值。 @Terente是的,我已經實現了它。 – Rahul 2011-06-06 10:05:40

回答

10

爲UIPickerView正常工作,你必須提供組件(列)的數量,每當它重新加載行每個組件的數量: 爲使用[myPickerView reloadAllComponents];在數組填充完成後重新加載視圖,但在聲明包含視圖控制器類爲<UIPickerViewDelegate>之後,您必須也執行這些事情,將拾取器作爲代理鏈接到文件所有者,然後:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 1;// or the number of vertical "columns" the picker will show... 
} 
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 
    if (myLoadedArray!=nil) { 
     return [myLoadedArray count];//this will tell the picker how many rows it has - in this case, the size of your loaded array... 
    } 
    return 0; 
} 

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { 
//you can also write code here to descide what data to return depending on the component ("column") 
     if (myLoadedArray!=nil) { 
      return [myLoadedArray objectAtIndex:row];//assuming the array contains strings.. 
     } 
     return @"";//or nil, depending how protective you are 
    } 
+1

不要忘記'UIPickerViewDataSource'協議 – 2016-12-11 14:38:07

1

更新數組中的值(插入或修改現有值)後。

致電reloadAllComponents您的UIPickerView實例。

- (void)reloadAllComponents 

爲了使用如下

[myPickerView reloadAllComponents]; 
相關問題