繁体   English   中英

UIpickerView的默认选定行的颜色

[英]the color for the default selected row of the UIpickerView

我有一个CityPickerView,它有2个组件供用户选择中国城市。我希望所选行的颜色为红色,未选择的行为同性恋,同时,pickerView的背景颜色为白色。

我已完成主要功能,但在运行应用程序时, 默认选定行的颜色不是红色。 怎么解决?

当用户选择组件0的行时,组件1的数据将自动重新加载,并且选择组件1的第一行。 组件0的行和组件1的第一行都是红色。

但是当用户选择组件1的第3行(或更多)时,则选择组件0的另一行。组件0的选定行为红色,但组件1的第一行不是红色,它仍然是同性恋的颜色。 我该如何解决?

并且,如果用户直接选择组件1的行, 如何使组件0的行也是红色?

UIPickerViewDelegate的代码如下:

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

}

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

如果您需要更多详细信息,我会在这里给出。谢谢。

您可以使用以下方法更改颜色:

- (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'参数应为'false'

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

如果它是'真',那么挑选者最终会到达你手动选择的行需要一些时间,延迟可以忽略颜色变化

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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