integers and uiimage in uipicker view - iphone

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;
}'
}

Related

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.

How I change text in UIPickerView from left to right

How I can change text in UIPickerView from leftside to rightsid.
for example if we suggest this is pickerview. I mean the text start from right side.
Instead of using the titleForRow delegate method of your picker view you should use viewForRow- that way you can return a UILabel that uses right justified text. If you just use the titleForRow method you don't have any control over how the text display, but if you use the latter you can customise it to your heart's content.
#lxt answer is right, but if he/she excuse me I'll put the full code
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel *lblPickerTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 30)] autorelease];
lblPickerTitle.text = [pickerArray objectAtIndex:row];
lblPickerTitle.font = [UIFont fontWithName:#"Arial" size:20];
lblPickerTitle.textAlignment = UITextAlignmentRight;
return lblPickerTitle;
}

Iphone Sdk: UIPickerView, possible to assign different font sizes to different components?

i was just wondering if there was a way to change the fontSize of a row in a uipickerview
for a specific component and not for all of them. Also the different components still have to be able to display different things and not the same.
Does anyone have a solution for this problem? Thanks in advance!
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *lbl = (UILabel *)view;
// Reuse the label if possible...
if ((lbl == nil) || ([lbl class] != [UILabel class])) {
CGRect frame = CGRectMake(0.0, 0.0, 270, 32.0);
lbl = [[[UILabel alloc] initWithFrame:frame] autorelease];
.... do what you like ...
}
lbl.textColor = [UIColor blackColor];
lbl.text = [self.<yourdata> objectAtIndex:row];
return lbl;
}
The calling parameters tell you which view, row and component you are setting. Check them and set the appropriate thing only if both the row and component match (compare and use flow control) what you want for that row and component.

UiPickerView change font color according data

I'm using a pickerView with multiple components related to several fields in a Database (CoreData).
Is it possible to change the fontcolor for a specific component according the presence of data in the DB ?
For example the field in the DB is null the component font color should be RED otherwise black.
Any help will be appreciated !
Dario
==================
Thanks Kenny,
I have to apply to a single UIPicker only. So I', returning the view parametere (without modificatiosn). The result is all the pickers show empty rows.
Thanks for help !
Here you will find the code fragment:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if (pickerView == tipoPk){
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100,30)];
label.textColor = [UIColor redColor];
switch (component) {
case PK_Tipo:
label.text = [tipoArray objectAtIndex:row]];
break;
case PK_Settore:
label.text = [settoreArray objectAtIndex:row]];
break;
default:
break;
}
return label;
}
else {
return view; // <==== return view for non related pickerviews , but no rows shown
}
}
This one also works..
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}
Yes, but you have to use -pickerView:viewForRow:forComponent:reusingView: and supply a custom UILabel.