简体   繁体   中英

the color for the default selected row of the UIpickerView

I have a CityPickerView which has 2 components for user to select the Chinese city.I want the color of the selected row is red and the unselected row is gay, meanwhile, the background color of pickerView is white.

I have completed the major function,but when run the app,the color of the default- selected row isn't red. How to solve it ?

When user select the row of the component 0,the data of the component 1 will reload automatically,and the first row of the component 1 is selected. Both of the row of the component 0 and the first row of the component 1 is red color.

But when user select the 3rd (or more) row of the component 1,then select another row of the component 0.The selected row of the component 0 is red,but the first row of the component 1 is not red,it is still gay color. How can I solve it ?

And, if user select the row of the component 1 directly , how to make the the row of the component 0 is also red color ?

The codes for the UIPickerViewDelegate is following:

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

    NSUserDefaults  *pickerSelectionDefaults=[NSUserDefaults standardUserDefaults];
    [pickerSelectionDefaults setInteger:row forKey:@"leftComponentSelectionKey"];
    [pickerSelectionDefaults setInteger:row forKey:@"rightComponentSelectionKey"];
    [pickerSelectionDefaults synchronize];

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


    if (component==0) {
        [pickerView reloadComponent:1];//if any row of the component 0 is selected,then reload the data of the component 1
        [pickerView selectRow:0 inComponent:1 animated:YES];//if the row of the component 0 is selected,then set the first  row of the component 1  is selected
        UILabel *label = (UILabel*)[pickerView viewForRow:0 forComponent:1];//get the label of the row 0 of component 1
        [label setTextColor:[UIColor redColor]];
    }

    [self changeTextField];

}

and

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

If you need more detail info.,I'll give it here.Thanks.

You can change the color by using this:

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

'animated' parameter should be 'false'

eg) 'pickerView.selectRow(row, inComponent: comp, animated: false)'

If it's 'true', it will take some time for the picker to eventually get to the row you manually selected and color change could be ignored by the delay

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM