How to Change Color selected Picked value from pickerview?

我与影子孤独终老i 提交于 2019-12-05 04:56:00

问题


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

I am trying to change the selected Picked Value Color from UIPickerView

I am trying to change it but its not effected perfectly its changing other values below the selected value.


回答1:


This is incorrect. You should reload the selected component. Then you need to fetch the selected row and update its color in viewForRow:forComponent:. Something like-

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


来源:https://stackoverflow.com/questions/7595220/how-to-change-color-selected-picked-value-from-pickerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!