2008-11-22 67 views
8

有誰知道如何更改行(或行背景)從iPhone SDK的UIPickerView控制顏色的?類似於下面的行標題,但我也想改變行的顏色:UIPickerView行顏色

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

謝謝。

回答

10

您可以在代理的-pickerView返回一個任意的觀點:viewForRow:forComponent:reusingView:方法,記錄here

14

感謝諾亞,這正是我需要的。我想在這裏添加代碼,以防別人需要(或希望在:)

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

不應該使用`reusingView`參數嗎?它類似於UITableView的重用池... – 2009-03-29 17:04:22

-7

評論做到這一點:

label.backgroundColor = [UIColor yourcolorColor]; 
3

我實現了基於肖恩的回答如下:

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

} 

而且

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