Цвет строки UIPickerView

Кто-нибудь знает, как изменить цвет строки (или фона строки) в элементе управления UIPickerView из iPhone SDK? Подобно приведенному ниже заголовку для строки, однако я также хотел бы изменить цвет строки:

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

Спасибо.


person Sean    schedule 22.11.2008    source источник


Ответы (5)


Вы можете вернуть произвольное представление в делегате -pickerView:viewForRow:forComponent:reusingView: метод, задокументированный здесь.

person Noah Witherspoon    schedule 22.11.2008

Спасибо, Ной, это именно то, что мне было нужно. Я хотел добавить код сюда на всякий случай, если кому-то еще понадобится (или захочется прокомментировать :)

- (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;
}
person Sean    schedule 22.11.2008
comment
Разве вы не должны использовать параметр reusingView? Это похоже на пул повторного использования UITableView... - person François P.; 29.03.2009
comment
Есть ли способ настроить панель выбора, которая отображается в виде серого градиента в UIPickerView? - person Raj Pawan Gumdal; 02.06.2010

Я реализовал следующее на основе ответа Шона:

- (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;
}
person Scott Gardner    schedule 23.08.2012

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

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

}

И

- (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;
}
person Ringo    schedule 02.11.2016

Сделай это:

label.backgroundColor = [UIColor yourcolorColor];
person Community    schedule 29.03.2009