我有以下问题:我可以更改uipicker内部行的文本颜色,但不能更改我想要的特定行的文本颜色,选择器会变得混乱,很多文本标签会变成蓝色。
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (UILabel*)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
retval.text = [arraySubSistemasDeSaude objectAtIndex:row];
if(pickerView == subSistemaSaudePicker) {
if ([retval.text isEqualToString:@"Seguros de saúde"]) {
NSLog(@"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
}
if ([retval.text isEqualToString:@"Sistemas de Saúde e Equiparados"]) {
NSLog(@"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
if ([retval.text isEqualToString:@"Seguradoras-Acidentes de Trabalho, viação/pessoais e vida"]) {
NSLog(@"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
}
if ([retval.text isEqualToString:@"Empresas, associações e outras entidades"]) {
NSLog(@"ROW=%d",row);
[retval setTextColor:[UIColor blueColor]];
}
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
}
} else if(pickerView == horarioPicker) {
retval.text =[arrayHorarios objectAtIndex:row];
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
} else if(pickerView == examesPicker) {
retval.text =[arrayExames objectAtIndex:row];
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
} else if(pickerView == consultasPicker) {
retval.text =[arrayConsultas objectAtIndex:row];
[retval setBackgroundColor:[UIColor clearColor]];
retval.font = [UIFont systemFontOfSize:14];
}else {
assert(NO);
}
return retval;
}
对不起我的英语不好,我希望我能提前清楚地说明这个问题。
答案 0 :(得分:3)
问题是,您正在重复使用每一行的视图,并且当它不应该是蓝色时,您不会将UILabel颜色设置为黑色。
添加此代码......
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (UILabel*)view;
if (!retval) {
retval= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
}
[retval setTextColor:[UIColor blackColor]];
retval.text = [arraySubSistemasDeSaude objectAtIndex:row];
......
这样,每次拉出重复使用的视图时,它首先会将颜色设置为黑色。然后代码将检查您想要的蓝色并将它们设置为蓝色。你这样做的方式是在其中一些上面设置蓝色,然后当你重复使用它们时,你永远不会把它重置为黑色。
答案 1 :(得分:1)
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *pickerLabel = (UILabel *)view;
if (pickerLabel == nil) {
CGRect frame = CGRectMake(0.0, 0.0, 80, 32);
pickerLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
[pickerLabel setTextAlignment:UITextAlignmentLeft];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
[pickerLabel setTextColor:[UIColor blackColor]];
[pickerLabel setText:@"MyTest"];
}
return pickerLabel;
}