如何从pickerview更改颜色选择的拾取值?

Posted

技术标签:

【中文标题】如何从pickerview更改颜色选择的拾取值?【英文标题】:How to Change Color selected Picked value from pickerview? 【发布时间】:2011-09-29 09:28:25 【问题描述】:
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 更改所选的选取值颜色

我正在尝试更改它,但它并没有完美地影响它更改低于所选值的其他值。

【问题讨论】:

pickerLabel 是 viewForRow 委托方法中使用的 UILabel。 【参考方案1】:

这是不正确的。您应该重新加载选定的组件。然后您需要获取选定的行并在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];
      

【讨论】:

以上是关于如何从pickerview更改颜色选择的拾取值?的主要内容,如果未能解决你的问题,请参考以下文章

如何更改 UIPickerView 中选定行的颜色

如何更改 UIPickerView 中选定行的颜色

在 Swift 中更改导航栏颜色

如何快速更改选择器视图的文本颜色? [复制]

如何根据第一个 pickerView 的选择更改我的第二个 pickerView 数组

在 Swift 上更改选择器视图的颜色