UIPickerView Row Color

Does anyone know how to change the color of a row (or row background) in the UIPickerView control from the iPhone SDK? Similiar to the below title for row, however I would also like to change the color of the row:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

Thank you.

Answers


You can return an arbitrary view in the delegate's -pickerView:viewForRow:forComponent:reusingView: method, documented here.


Thanks Noah, that's exactly what I needed. I wanted to add the code here just in case anyone else needs (or wants to comment on :)

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

I implemented the following based on Sean's answer:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    CGRect rowFrame = CGRectMake(00.0f, 0.0f, [pickerView viewForRow:row forComponent:component].frame.size.width, [pickerView viewForRow:row forComponent:component].frame.size.height);
    UILabel *label = [[UILabel alloc] initWithFrame:rowFrame];
    label.font = [UIFont boldSystemFontOfSize:18.0f];

    // This is an array I pass to the picker in prepareForSegue:sender:
    label.text = [self.values objectAtIndex:row];
    label.textAlignment = UITextAlignmentCenter;

    // This is an array I pass to the picker in prepareForSegue:sender:
    if ([self.backgroundColors count]) {
        label.backgroundColor = [self.backgroundColors objectAtIndex:row];

        // self.lightColors is an array I instantiate in viewDidLoad: self.lightColors = @[ [UIColor yellowColor], [UIColor greenColor], [UIColor whiteColor] ];
        label.textColor = [self.lightColors containsObject:label.backgroundColor] ? [UIColor blackColor] : [UIColor whiteColor];
    } else {
        label.textColor = [UIColor blackColor];
    }

    return label;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
    [labelSelected setTextColor:[UIColor redColor]];

}

And

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    UILabel *label = (id)view;

    if (!label){

      label=[[UILabel alloc]init];
      label.textAlignment = NSTextAlignmentCenter;
      pickerView.backgroundColor=[UIColor whiteColor];
      label.text=[self pickerView:pickerView titleForRow:row forComponent:component];
      label.textColor=[UIColor grayColor];

    }
    return label;
}

Do this:

label.backgroundColor = [UIColor yourcolorColor];

Need Your Help

AudioQueue callback in simulator but not on device

iphone objective-c ios core-audio audioqueueservices

I am currently working on an audio processing app on iPhone. it is based on Apple's SpeakHere sample code, aims at real-time audio processing and playback. The code works well in simulator, but not

Crucible: No listener invokers were found for listener

java events atlassian-crucible atlassian-plugin-sdk

I'm trying to catch Review state event, like when somebody is closing it. And after to get any signs of life from the plug-in. To send a line to the log file (from any log level) or to STDOUT.