Custom picker view with images - iphone

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.

Related

How to make my UIPickerView display all elements of an array

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]);
}

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/

integers and uiimage in uipicker view

i currently have a uipicker with two components. The first contains images and works fine, the second however requires integer values.
Is this possible since the following callback method returns a UIView type not integer? Surely there must be a way around this?
-(UIView *)pickerView
any help would greatly be appreciated, many thanks
this?
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
switch (component) {
case 1:
{
UILabel *intvalue = view?(UILabel *)view:[[[UILabel alloc] initWithFrame:CGRectMake(0,0, 50, 40)] autorelease];
intvalue.text = [NSString stringWithFormat:#"%d",value];
intvalue.textAlignment = UITextAlignmentCenter;
intvalue.backgroundColor = [UIColor clearColor];
return intvalue;
break;
}'
}

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.