2011-09-29 51 views
3
pickerArray = [[NSArray arrayWithObjects:@"20", @"40", @"60",@"80", @"100", @"120", @"140", @"160", @"180", @"200",nil] retain]; 

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

     //On Selecting the component row 
     if (component == 0) { 

     } else if (component == 1) { 

      [quantityPickerDelegate didChangeLabelText:[pickerArray objectAtIndex:row]];// delegate passing the selected value 
      pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1]; 

    } 


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

    //Picker view content added 
    pickerLabel = [[UILabel alloc] init]; 
    pickerLabel.backgroundColor=[UIColor clearColor]; 

    if (component == 0) 
    { 
     [pickerLabel setText:@"Total"]; 
     pickerLabel.frame = CGRectMake(0.0, 200,220,44); 
     pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1]; 
    } 

    else 
    { 
     pickerLabel = [[UILabel alloc] init]; 
     pickerLabel.backgroundColor=[UIColor clearColor]; 
     pickerLabel.frame = CGRectMake(0.0, 200,70,44); 
     [pickerLabel setText:[pickerArray objectAtIndex:row]]; 
     pickerLabel.textColor = [UIColor blackColor]; 
    } 

    [pickerLabel setTextAlignment:UITextAlignmentCenter]; 
    pickerLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20]; 
    pickerLabel.font = [UIFont boldSystemFontOfSize:20]; 

    //return [pickerLabel autorelease]; 

    return pickerLabel; 
} 

我試圖從UIPickerView如何更改顏色從pickerview中選擇Picked值?

改變選擇的攝數值顏色我試圖去改變它,但它不是影響它的完美改變低於選定值以外的值。

+0

pickerLabel是在的UILabel委託viewForRow方法中使用。 – user891268

回答

5

這是不正確的。您應該重新加載選定的組件。然後您需要獲取所選行並更新其顏色viewForRow:forComponent:。東西喜歡 -

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

     //On Selecting the component row 
     if (component == 0) { 

     } else if (component == 1) { 

      [quantityPickerDelegate didChangeLabelText:[pickerArray objectAtIndex:row]];// delegate passing the selected value 
      [pickerView reloadComponent:component]; //This will cause your viewForComp to hit 
     } 
    } 

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    //... 
    //Your usual code 
     pickerLabel.textColor = defaultColor; 

    if([self.pickerView selectedRowInComponent:component] == row) //this is the selected one, change its color 
    { 
      pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1]; 
    } 
} 

HTH,

阿克沙伊