2012-08-03 6 views
2

다음과 같은 문제가 있습니다. uipicker 내부 행의 텍스트 색을 변경할 수 있지만 원하는 특정 행의 텍스트 색을 변경할 수 없으며 선택기가 혼란스럽고 많은 텍스트 레이블이됩니다. 푸른.특정 행 색상 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; 
} 

영어로 불편을 끼쳐 드려 죄송합니다. 미리 감사드립니다.

답변

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

는, 그것이 다시보기를 끌어마다 먼저 검은 색으로 색상을 설정합니다. 그런 다음 코드는 원하는 파란색이 파란색인지 확인하고 파란색으로 설정합니다. 당신이 그것을하고 있던 방법, 그들 중 몇몇에 파란 색깔을 놓고 있었다, 그 때 당신이 그들을 재사용하기 위하여 갈 때 당신은 결코 그것을 까만에 다시 놓고 있지 않았다.

+0

저스틴 님, 완벽하게 작동했습니다. :) –

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; 

}