2011-06-06 57 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]; 
相关问题