Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill UIPickerView with an Array Value

Tags:

iphone

ipad

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.) Take 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.

like image 942
Rahul Avatar asked Feb 14 '26 02:02

Rahul


1 Answers

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
    }
like image 136
RabinDev Avatar answered Feb 16 '26 17:02

RabinDev