Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 507
Sean Avatar asked Nov 22 '08 02:11

Sean


2 Answers

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;
}
like image 164
Sean Avatar answered Nov 15 '22 22:11

Sean


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

like image 29
Noah Witherspoon Avatar answered Nov 15 '22 20:11

Noah Witherspoon