2012-08-23 5 views
0

피커 뷰 객체 색상을 연속적으로 변경하는 방법이 있습니까?피커 뷰에서 행 색상 변경

예를 들어 addobject:@"black"이 있지만 빨간색으로 글꼴 색상을 변경하는 방법은 무엇입니까?

답변

1

"제목"대리자 메서드를 사용하는 대신 UIView 메서드를 사용하여 사용자 자신의보기를 만들고 채 웁니다. 그래서 너비가 320 인 UIView를 44 개 만들어서 레이블을 넣고 라벨 텍스트와 색상을 설정하면 이제 원하는 것을 정확하게 얻을 수 있습니다.

편집 :

#define PICKER_VIEW_SIZE CGSize(300, 40) 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 
{ 
    return PICKER_VIEW_SIZE.height; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 
{ 
    return PICKER_VIEW_SIZE.width; 
} 

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 
{ 
    // assume one component, but this solutions works for multiple 
    // lets do red, green, and blue alternating backgrounds with alternating black and white text 

    UIColor *backgroundColor; 
    switch(row % 3) { 
    case 0: 
    default: 
     backgroundColor = [UICOlor redColor]; 
     break; 
    case 1: 
     backgroundColor = [UIColor blueColor]; 
     break; 
    case 2: 
     backgroundColor = [UIColor greenColor]; 
     break; 
    } 
    UIColor *textColor = (row % 2) ? [UIColor blackColor] : [UIColor whiteColor]; 

    UIView *view = [[UIView alloc] initWithFrame:(CGRect){ {0,0}, PICKER_VIEW_SIZE}]; 
    view.backgroundColor = backgroundColor; 

    UILabel *label = [UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 20)]; 
    // configure the label 
    label.text = text appropriate for this row; 

    [view addSubview:label]; 

    return view; 
} 
+0

예 내가 그 방법을 알고하지만 난 모든 라인 (행) 다른 색깔을 가지고 싶습니다. 그게 가능한가? –

+0

물론 가능합니다. 행에 대한 뷰를 요청 받았을 때 어떤 색으로 만들 것인지 결정합니다. 이 위임 메서드는 열과 행이 무엇인지 알려줍니다. 각기 다른보기를 만들 수 있습니다 - 다른 배경색, 글꼴, 텍스트 색 등을 사용하십시오. 원하는대로 할 수 있습니다! –

+0

몇 가지 예를 게시 할 수 있습니까? –