2008-11-22 26 views
8

¿Alguien sabe cómo cambiar el color de una fila (o fondo de fila) en el control UIPickerView desde el iPhone SDK? Similar al título por debajo de la fila, sin embargo, me gustaría también cambiar el color de la fila:Color de fila UIPickerView

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

Gracias.

Respuesta

10

Puede devolver una vista arbitraria en el delegado -pickerView: viewForRow: forComponent: reusingView: método, documentado here.

14

Gracias Noah, eso es exactamente lo que necesitaba. Quería añadir el código aquí por si alguien más necesita (o quiere hacer comentarios sobre :)

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

¿No debería usar el parámetro 'reusingView'? Es similar al grupo de reutilización de UITableView ... –

+0

¿Hay alguna manera de personalizar la barra de selección que se muestra como degradado gris en un UIPickerView? –

-7

Haga lo siguiente:

label.backgroundColor = [UIColor yourcolorColor]; 
3

I implementado la siguiente basado en la respuesta de Sean:

- (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; 
} 
0
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 

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

} 

Y

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