Dynamically Add values to UIPickerView at run time - iphone

How can you dynamically add values to UIPickerView at runtime.
I'm using the following code to populate a UIPickerView statically. Need to add
values dynamically at run time, for e.g. Three, Four etc.
- (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title = nil;
if(row==0){
title = #"One";
}
if(row==1){
title = #"Two";
}

If you had an array of the titles, you could use something like:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [titles objectAtIndex:row];
}
The titles will be refreshed when you call [pickerView reloadAllComponents] (or reloadComponent: if you have more than one column and only want to refresh one of them).

Hey I had a problem with UIPickerView..... when I call reloadAllComponents it wasn't calling widthForComponent, but it was calling other delegate methods such as titleForRow.... I have no idea why this is, perhaps apple doesn't want the width changing dynamically? anyway it was on another screen so I didn't have the animation/no-animation issue. I found that by resetting the delegate of the picker, I could get widthForComponent to be called again!
i.e.
picker.delegate = view;
where view implements UIPickerViewDelegate.... hope this helps someone out there!

Related

Custom UIPickerView

I want to use custom picker view that will add a check sign in front of selected row.
I have used apple sample code for custom UIPickerView on the UICatalog example. I was able to add the check sign for a given row when creating the picker. But I failed to add it when the user rotate the wheel to select new row and also to remove it from the previously added row. Any help would be appreciated.
Thanks,
1) Create subclass of UIView that will represent row in picker. Define property, for example, isChecked, which will show/hide checkmark on this view
2) In the – pickerView:didSelectRow:inComponent: call – viewForRow:forComponent: for previously selected row, set isChecked=NO
3) call – viewForRow:forComponent: for currently selected row and set isChecked=YES;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
MyCustomRowView *prevRowView = [pickerView viewForRow:currentlySelectedRow forComponent:component];
prevRowView.isChecked = NO;
MyCustomRowView *currentRowView = [pickerView viewForRow:row forComponent:component];
currentRowView.isChecked = YES;
//then save currently selected row
currentlySelectedRow = row;
}
4) you also should check currently selected row when you is requested for it:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
....
//Create or reuse view
....
rowView.isChecked = (row == currentlySelectedRow);
}
As you are not providing any code, all we can give you is a general advise;
make sure the viewController showing your UIPickerView instance is inheritating the UIPickerViewDelegate-protocol
set the delegate of your UIPickerView towards this viewController; e.g. pickerView.delegate = self;
implement - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
within the above implementation, remove any checkmark previously added and add a new one to the row selected.

UIPicker noob question - If I click on first item in the picker, the result is null - all other items are fine

I have a UIPicker with 7 items in it. (only one component)... If I click on the first item in the Picker (without ANY SCROLLING of the picker), the text results I get back are null.
However, If I so much as "tug" downward on the list and THEN CLICK item #1 it works fine.
If I click ANY other item, it always returns the right result. Of course, the list has to scroll in order for those items to get selected.
What is happening to cause this? Is there a delegate function I need to call? Is there some way to default the selection to the first item in the list, if they never click anything?
Thanks,
Phil
Here is a snippet from "viewdidload" in my .m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#">>> Entering %s <<<", __PRETTY_FUNCTION__);
listOfTerms=[[NSArray alloc]initWithObjects:
#"Fall Semester",#"Spring Semester",#"Summer Session",#"Q1",#"Q2",#"Q3",#"Q4",nil];
...
...
Here are the other 4 UIPickerView delegate functions I have defined.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
return [listOfTerms count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [listOfTerms objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
selectedTerm =[[NSString alloc]
initWithFormat:#"%#",
[listOfTerms objectAtIndex:row]];
}
pickerView:didSelectRow:inComponent: is only called when the picker view scrolls. So if you never scroll the view, the method never gets called and therefore selectedTerm is never set. The fix is simple, initialize selectedTerm in viewDidLoad:.
The most simple answer for all situation is to imply didSelectRow in viewDidAppear
(Please note that it is NOT viewDidLoad method)
- (void) viewDidAppear:(BOOL)animated{
[self pickerView:[self yourpickerview] didSelectRow:0 inComponent:0;
}

how to add picker view programmiticaly for list of image names

Im new to iphone development.In my application i want to add image picker for list of image names.here i added picker view and i dont no how to add list of image names programmiticaly in iphone with out using IB.
can any one plz give me code for displaying image names programmiticaly in pickerview.
Thankyou in advance.
#user549767 the answer of dks1725 is correct way to give images to your pickerview.....but in order to add pickerView programatically follow the below link....
UIPickerview in iphone
in viewDidLoad method make an array of your image names like below
-(void)viewDidLoad
{
arr=[[NSArray alloc] initWithObjects:#"image_name1",#"image_name2",#"image_name3",nil];
}
Now in picker view's delegate method write below code
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [arr count];
}
-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [arr objectAtIndex:row];
}

how to reload the picker view with new data

I am loading the picker view first time with the values which stored in array.
I am using,
temp = [NSArray arrayWithObjects:#"Male", #"Female", nil];
- (NSString *)pickerView:addData titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [temp objectAtIndex:row];
}
But it shows the empty picker view.
Did you try [somePickerVier reloadAllComponents];?
Also, the correct delegate method is - (NSString *)pickerView: (UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component.
Make sure that you set the delegate and datasource?
If not,
You have to implement this in your .h file
#interface YourView : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
u have been using autorelease array
so u have to retain this if u want
u dont like this
then do alloc and init then
add male,female...
temp = [[NSArray arrayWithObjects:#"Male", #"Female", nil]retain];
(NSString *)pickerView:addData titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
return [temp objectAtIndex:row];
}

Very simple iPhone question. How to make UIPickerView with two columns?

I have read other questions about creating a UIPickerView with two or more columns but cannot find the exact solution.
How is this done for the iPhone programmatically? How do you add static data? Thanks!
Make your controller (or whatever is controlling the behavior of the PickerView) support the UIPickerViewDelegate protocol. Then, implement:
- (int) numberOfColumnsInPickerView:(UIPickerView*)picker
to return the number of columns you want, and
- (int) pickerView:(UIPickerView*)picker numberOfRowsInColumn:(int)col
to return the number of rows for each column, and finally:
- (UIPickerTableCell*) pickerView:(UIPickerView*)picker tableCellForRow:(int)row inColumn:(int)col
to setup each cell.
See the reference for UIPickerView and UIPickerViewDelegate.
There is an excellent tutorial on this subject here.
Assuming you have a dictionary or a couple of arrays holding your static data. For simplicity I will go with a very simple array.
You have to modify your view controllers interface definition to tell the program that your view controller can provide data and delegation to a picker view.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
Than just implementing a couple of methods will make it work however documentation should be checked for other methods that are optional but provides more customization and control.
#interface NVHomeViewController : UIViewController <UIPickerViewDelegate,UIPickerViewDataSource>
NSArray *options;
- (void)viewDidLoad
{
[super viewDidLoad];
options = #[#"a",#"b",#"c",#"d"];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [options count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [options objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog(#"%# selected.",[options objectAtIndex:row]);
}