How to make my UIPickerView display all elements of an array - iphone

I have a UIPickerView and an array though I can't seem to be able to input all the date from the array into the UIPicker.
I know that the syntax is meant to be like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
Where treatments is the array name though when I use this it comes up with this error:
-[Treatment isEqualToString:]: unrecognized
I have searched my entire project and can't find the phrase: isEqualToString
Treatment.h file:
#import <Foundation/Foundation.h>
#interface Treatment : NSObject {
NSString *treatmentid;
NSString *treatmentName;
NSString *treatmentPrice;
}
#property (nonatomic, strong) NSString *treatmentid;
#property (nonatomic, strong) NSString *treatmentName;
#property (nonatomic, strong) NSString *treatmentPrice;
#end
All picker codes:
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [treatments count];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [treatments objectAtIndex:row];
}
If you need any more code just say
Thanks in advance

you your Array contain Dictionar or NSMutableDictionar then you must specify value of array with it's key like bellow try to writtern you method like this:
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return[[treatments objectAtIndex:row]valueForKey:#"YourKey"];
}
Here is basic Implementation of Picker view sample example code:-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thepickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thepickerView numberOfRowsInComponent:(NSInteger)component
{
return [treatments count];
}
customize displaying lable of UIpickerview like bellow:-
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
if (component == 0) {
label.font=[UIFont boldSystemFontOfSize:22];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.text = [NSString stringWithFormat:#"%d", row];
label.font=[UIFont boldSystemFontOfSize:22];
NSLog(#"%#",[yourpickerview selectedRowInComponent:component]);
}
return label;
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"%#",[treatments objectAtIndex:row]);
}

Related

How to use UIPickerView on second ViewController

" I am creating a application for IPhone in which I have to creat a login page. after getting the response from the server..it will navigate the user to next view. in next view, i m using a UIPickerView. but it is not working.. becoz i only know that how to use it in individual app . but i dont knw how to use it in second ViewController of the application??"
You can getPicker using this
-(void)getValuePicker
{
ViewForValuePicker = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
toolBar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneBtnPressToGetValue)];
[toolBar setItems:[NSArray arrayWithObject:btn]];
[ViewForValuePicker addSubview:toolBar];
valuePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 320, 216)];
valuePicker.delegate=self;
valuePicker.dataSource=self;
valuePicker.showsSelectionIndicator=YES;
[ViewForValuePicker addSubview:valuePicker];
[appDelegate.window addSubview:ViewForValuePicker];
}
And its Delegete Method
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [pickerValueAry count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
NSMutableArray *ary = [[NSMutableArray alloc] initWithArray:pickerValueAry];
id str=[ary objectAtIndex:row];
return str;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(#"selectedRowInPicker >> %d",row);
}
in .h file this method
UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
{
IBOutlet UIPickerView *picker;
NSMutableArray *pickerArrayType;
}
//in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
pickerArrayType = [[NSMutableArray alloc] initWithObjects:#"Type 1",#"Type 2",#"Type 3",#"Type 4",#"Type 5", nil]
}
# pragma mark - picker Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (void)pickerView:(UIPickerView *)pickerView1 didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
lbl.text = [pickerArrayType objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
return [pickerArrayType count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [pickerArrayType objectAtIndex:row];
}

UIPickerView pickerView:viewForRow:forComponent:reusingView: does not affect below iOS 5.0

My UIPickerView retrieves it's data using the pickerView:viewForRow:forComponent:reusingView: method.
For some reason the method doesn't affect on iOS 4.3 and below.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel* label = (UILabel*)view;
if (view == nil) {
label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
}
label.text = #"Text";
return label;
}
This guy has made an example where he states that the view should not be autoreleased. http://alisothegeek.com/2009/07/custom-uipickerview-text-formatting/

Custom picker view with images

I Learned how to use Picker views, singles and doubles. I would like to learn how to do a Picker view with images. Instead of numbers or words the scroll have different images to select one. Thanks. I think is using this code but it osent run me, I cant find the mistake. Thanks for your help.
#synthesize picker, Array, image;
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component;
{
return [self.Array count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [self.Array objectAtIndex:row];
}
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger)component reusingView:(UIView *)view
{
NSString *img_src = [NSString stringWithFormat:#"6-12AM.png", row];
UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]];
return image;
}
EDIT: I confused the function you need to implement in my initial answer. Sorry for that.
You need to implement the
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
method of your UIPickerView delegate to return an UIImageView for a given row.
Reference page here.
for example (very dumb implementation without reusing views)
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
NSString * img_src = [NSString stringWithFormat:#"image_%d.png", row];
UIImageView * retval = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:img_src]] autorelease];
return retval;
}
assuming you have images named image_[num].png in your resources.

Getting a "?" when populating my pickerview on load

I've searched here, but didn't see nothing like this error.
I'm trying to populate my pickerview on ViewDidLoad, but when I run my program, the pickerView is just populated with "?" instead my strings... what's going on here? Thanks in advance.
- (void)viewDidLoad {
[super viewDidLoad];
activities = [[NSArray alloc] initWithObjects:#"sleeping", #"eating", #"working", #"thinking", #"crying", #"begging", #"leaving", #"shopping", #"hello worlding", nil];
feelings = [[NSArray alloc] initWithObjects:#"awsome",#"sad",#"happy",#"ambivalent",#"nauseous",#"psyched",#"confused",#"hopeful",#"anxious",nil];
}
You need to implement the UIPickerViewDelegate and UIPickerViewDataSource protocols:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0)
return [activities count];
else
return [feelings count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component == 0)
return [activities objectAtIndex:row];
else
return [feelings objectAtIndex:row];
}

How to give this picker view a datasource

Here I am adding a pickerview programaticly
- (void)viewDidLoad {
[super viewDidLoad];
CGRect pickerFrame = CGRectMake(0,280,321,200);
UIPickerView *myPickerView = [[UIPickerView alloc] init]; //Not sure if this is the proper allocation/initialization procedure
//Set up the picker view as you need it
//Set up the display frame
myPickerView.frame = pickerFrame; //I recommend using IB just to get the proper width/height dimensions
//Add the picker to the view
[self.view addSubview:myPickerView];
}
But now I need to actually have it display content and somehow find out when it changes and what value it has changed to. How do I do this?
in .h file place this code
#interface RootVC : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>
assign the datasource and delegate to the picker
// this view controller is the data source and delegate
myPickerView.delegate = self;
myPickerView.dataSource = self;
use the following delegate and datasouce methods
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
return 200;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 50;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *returnStr = #"";
if (pickerView == myPickerView)
{
returnStr = [[levelPickerViewArray objectAtIndex:row] objectForKey:#"nodeContent"];
}
return returnStr;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (pickerView == myPickerView)
{
return [levelPickerViewArray count];
}
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
You need to create a class that implements the UIPickerViewDataSource protocol and assign an instance of it to myPickerView.dataSource.