Fill UIPickerView with an Array Value
i am working on a project in which i have to perform following two work: 1.) Fetch value from CoreData and store it in an NSMutableArray. 2.) Tak开发者_开发百科e a UIPickerView and fill it with an Array value.
Problem is that size of array is dynamic and i canto fill an array value in UIPickerView. can someone help me.
in order for the UIPickerView to work correctly, you must supply the amount of components (columns) and the number of rows for each component whenever it reloads:
as mentioned, use [myPickerView reloadAllComponents];
to reload the view once the array is populated, but you MUST implement also these things after you declare the containing view controller class as <UIPickerViewDelegate>
link the picker to the file owner as a delegate, and then:
- (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
}
After updating the value (Inserting or modifying existing) in your array.
Call reloadAllComponents
on your UIPickerView
instance.
- (void)reloadAllComponents
Use as below
[myPickerView reloadAllComponents];
精彩评论