2012-02-22 3 views
3

내 viewController에 여러 UIPickerView가 있습니다.여러 UIPickerView

그리고이 중 하나는 2 개의 UILabels를 1 개의 피커 행에 표시하므로 사용자 정의해야합니다.

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component { 
    // this method is use for normal pickers, and I would judge whether the picker that calling 
    // this method is a normal one or the special one by pickerView. 
} 
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
forComponent:(NSInteger)component reusingView:(UIView *)view { 
    // and this one is specially for the special picker, I would also judge the pickerView is 
    //normal or special, if pickerView is normal, return nil, else I return a UIView with 
    //2 UIlabels. 
} 

하지만 지금 내 디버깅 후, 내가 함께이 방법을 구현하는 경우, 두 번째는 항상 전화를 첫 번째는 호출 할 수 결코 보인다 발견 :

는 그리고 나는이 위임 방법을 사용

그리고 내 특수 피커가 올바른 데이터를 표시하지만 다른 것들은 아무 것도 없습니다.

어떻게하면됩니까?

두 번째 방법에서 모든 선택기 데이터를 제공하면 특수 선택기의 reusingView가 다른 선택기와 같은 형식이 아니기 때문에 reusingView가 문제가됩니까?

고맙습니다.

+0

흥미로운 점은 두 번째 방법에서 다른 선택기에 대해 nil을 반환합니까? – govi

+0

네, 문제가 되겠습니까? 내가 아무것도 돌려주지 않으면 경고가 생기므로 ... 나는 그 코드를 즉시 삭제하려고 노력할 것이다. –

+0

잘하고 있습니다. 그 줄을 삭제할 필요는 없습니다. – govi

답변

1

그러나 다른 대표자를 사용하는 것은 그다지 간단하지 않습니다. 이렇게되면,이 경우에는,

@interface MyCustomDelegate : NSObject <UIPickerViewDelegate> 

을 별도의 클래스를 만들고 대리자 메서드를 구현하는 것은

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row 
forComponent:(NSInteger)component { 
    // this method is use for normal pickers, and I would judge whether the picker that calling 
    // this method is a normal one or the special one by pickerView. 
} 

..

이 클래스의 인스턴스를 생성하고 같은 대리자로 설정
pickerView.delegate = [[MyCustomDelegate alloc] initWithData:data] autorelease]; 

여기서 데이터는 제목에 대한 대리자 메서드에 사용하려는 데이터 일 수 있습니다.

모든 피커에 대해 동일한 위임 인스턴스를 사용하거나 별도의 인스턴스를 만들 수 있습니다. 당신이 가지고있는 데이터의 종류에 달려 있습니다. 그것들이 모두 서로 관련이 없다면 별도의 인스턴스를 사용하는 것이 좋을 것입니다. 그렇지 않으면 당신은 하나만 사용할 수 있습니다.

글쎄. 당신이 당연히이

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
    if (pickerView==specialPickerView) { 

    } 
    else { 
     UILabel *lbl = [[UILabel alloc] init]; 
     lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component]; 
     //do some more styling like setting up the font as bold 
     //adding some padding to the text and some shiny things 
     return lbl; 
    } 
} 

같은 일을 할 것입니다 두 번째,에 관해서는


, 당신은 당신의 기존 클래스에이 일을 할 것입니다.

1

위의 응답에 'govi'가 추가됩니다. 'tag'속성 (이 경우 각 UIPicker보기에 다른 '태그'값을 지정해야합니다. 즉 1,2,3 ...)을 사용하여 여러 UIPikeView를 식별 할 수도 있습니다. 예를 들어,

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 
if (pickerView.tag ==0) { 

} 
else if (pickerView.tag ==1) { 
    UILabel *lbl = [[UILabel alloc] init]; 
    lbl.text = [self pickerView:pickerView titleForRow:row forComponent:component]; 
    //do some more styling like setting up the font as bold 
    //adding some padding to the text and some shiny things 
    return lbl; 
} 

}

가 더욱 더 당신이이

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if (pickerView.tag ==0) { } 
else if (pickerView.tag ==1) { } } 

중 하나를 사용할 수 있습니다 이것은 u는 당신이 선택 도구는 단순히 몇 가지 값을 표시하고자 할 때 특히 사용 하나입니다.위 라벨은 표시된 라벨 (예 : 너비, 높이, 글꼴 ...)에 맞춤 설정을 추가하려는 경우에 사용됩니다.