Populate UIPickerView with array value

I am working on a project in which I have to do the following two work: 1.) Extract the value from CoreData and save it in NSMutableArray. 2.) Take a UIPickerView and fill it with an Array value.

The problem is that the size of the array is dynamic, and I canto the value of the array in UIPickerView. can someone help me.

+3
source share
2 answers

for UIPickerView to work correctly, you must specify the number of components (columns) and the number of rows for each component each time you reload: as mentioned, use [myPickerView reloadAllComponents];to reload the view after filling the array, but you MUST implement these things after declaring the class of the view controller as <UIPickerViewDelegate>associate the collector with the owner of the file 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
    }
+10
source

After updating the value (Insert or modify an existing one) in your array.

Call reloadAllComponentson your copy UIPickerView.

- (void)reloadAllComponents

Use as below

[myPickerView  reloadAllComponents];
+1
source

All Articles