3

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.

3
  • What is UIPickercontroller ? Commented Jun 6, 2011 at 9:57
  • What you have tried till now? You are using UIPickerView? Did you implement the delegate methods for it? Commented Jun 6, 2011 at 10:02
  • @jhaliya UIPickercontroller is use to show an array value. @Terente Yes i have Implemented it. Commented Jun 6, 2011 at 10:05

2 Answers 2

10

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
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget about UIPickerViewDataSource protocol
1

After updating the value (Inserting or modifying existing) in your array.

Call reloadAllComponents on your UIPickerView instance.

- (void)reloadAllComponents

Use as below

[myPickerView  reloadAllComponents];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.