2009-06-05 5 views
8

UIPickerView에서 강조 표시된 선택 항목 재정의

-(UIView *)pickerView:(UIPickerView *)pickerView 
     viewForRow:(NSInteger)row 
    forComponent:(NSInteger)component 
     reusingView:(UIView *)view 

UILabels로 피커를 채 웁니다. 터치했을 때 선택한 행을 강조 표시하는 동작을 비활성화하는 방법이 있습니까?

이것은 UIPickerView에 내재 된 기본 UITableViewCell의 속성이라고 생각하며 변경 방법을 찾을 수 없습니다.

답변

16

당신은 사용자 정의보기에는 다음과 같은 속성이 있는지 확인해야합니다

  1. 그것은 UIPickerView가 대리자 방법을 기반으로 기대하는 같은 크기로 크기해야 - pickerView:rowHeightForComponent:pickerView:widthForComponent:. 맞춤 높이를 지정하지 않으면 기본 높이가 44입니다.
  2. 배경색은 [UIColor clearColor]이어야합니다.
  3. 보기는 모든 터치를 캡처해야합니다.

하나 개 잡았다 정의보기로 UILabel 인스턴스를 사용중인 디폴트 UILabeluserInteractionEnabled-NO (UIView 반면, 디폴트 YES이 속성).

Halle의 예제 코드는 다음과 같이 다시 작성 될 수 있습니다. 또한이 예제는 빠른 스크롤링 성능에 필요한 이전에 작성한 뷰를 올바르게 재사용합니다.

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

    UILabel *pickerRowLabel = (UILabel *)view; 
    if (pickerRowLabel == nil) { 
    // Rule 1: width and height match what the picker view expects. 
    //   Change as needed. 
    CGRect frame = CGRectMake(0.0, 0.0, 320, 44); 
    pickerRowLabel = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
    // Rule 2: background color is clear. The view is positioned over 
    //   the UIPickerView chrome. 
    pickerRowLabel.backgroundColor = [UIColor clearColor]; 
    // Rule 3: view must capture all touches otherwise the cell will highlight, 
    //   because the picker view uses a UITableView in its implementation. 
    pickerRowLabel.userInteractionEnabled = YES; 
    } 
    pickerRowLabel.text = [pickerDataArray objectAtIndex:row]; 

    return pickerRowLabel; 
} 
0

는 내가 선택 피드백을 제거 할 수있는 쉬운 방법이 있는지 당신은 잘 모르겠어요 UIPickerView

+0

showsSelectionIndicator는 선택기에서 반환 할 값 위에 파란색 막대를 표시합니다. 불행히도 기본 UITableViewCell 구조체와 아무런 관련이 없습니다. – Jon

0

의 "showsSelectionIndicator"속성을보고 할 수 있습니다 생각하지만, 당신이 배경을 만들 경우 당신은 그것을 커버 할 수 청색 선택 사각형과 동일한 치수 흰색과 크기를 라벨 :

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

    UILabel *pickerRowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 316, 40)]; 
    pickerRowLabel.backgroundColor = [UIColor whiteColor]; 
    pickerRowLabel.text = [pickerDataArray objectAtIndex:row]; 
    [self.view addSubview:pickerRowLabel]; 

    return pickerRowLabel; 

} 

(316)의 폭을 갖는 라벨 양쪽 청색 슬리버 남기고 모두 커버 (320)에 완전히 선택 커버 피드백하지만 그것은 또한 너를 괴롭히지 않을 수도있는 바깥 쪽 바퀴 구배의 약간을 포함하기 시작한다.

12

UILabelYES에 수정 하이라이팅 문제 userInteractionEnabled의 속성을 설정하지만, 그것은 또한 터치 한 행을 선택하는 자동 스크롤로부터 UIPickerView 비활성화. 당신이 강조 동작을 비활성화하지만 UIPickerView의 기본 자동 스크롤 기능을 유지하려면

UIPickerView에 포함 된 UITableCell 경우에 setShowSelection 함수를 호출합니다. -

#import <UIKit/UIKit.h> 

@interface PickerViewLabel:UILabel 
{ 
} 

@end 

PickerViewLabel

PickerViewLabel.h :이 일을하는 방법은 다음과 유사한 UILabel 클래스를 서브 클래 싱하는 것입니다.m - 그럼

#import "PickerViewLabel.h" 

@implementation PickerViewLabel 

- (void)didMoveToSuperview 
{ 
if ([[self superview] respondsToSelector:@selector(setShowSelection:)]) 
{ 
    [[self superview] performSelector:@selector(setShowSelection:) withObject:NO]; 
} 
} 

@end 

, 이전 pickerView:viewForRow:forComponent:reusingView:UILabel의 인스턴스를 반환했다, PickerViewLabel의 인스턴스를 돌려줍니다. 예를 들어 Doug의 코드를 사용하면 'UILabel'의 모든 사례를 'PickerViewLabel'으로 바꿀 수 있습니다. pickerRowLabel.userInteractionEnabled = YES; 행을 삭제해야합니다.

+0

안녕하세요 크리스틴, 위의 코드를 구현했지만 파란색 선택은 선택한 행의 양쪽 끝에 있습니다. 코드를 디버깅하려고하면 didMoveToSuperview가 어디에서도 호출되지 않는다는 것을 알았습니다. 너 나 좀 도와 줄 수있어? 감사. – engineer

+0

Hey Shailesh, 코드를 보지 않고 말하기는 어렵지만, 왜 호출되지 않을지에 대한 몇 가지 아이디어가 있습니다. 'didMoveToSuperview'는 UIView가 뷰에 추가되거나 제거 될 때마다 자동으로 호출되는 UIView의 함수입니다. 사용자 정의보기가 화면에 확실히 추가됩니까? 빠른 테스트를하고 클래스의 인스턴스를 UIViewController의 뷰의 하위 뷰로 추가하면 'didMoveToSuperview'가 호출되는 것을 보시겠습니까? 그렇지 않다면 함수 선언 어딘가에 오타가 있다고 생각합니다. – Christine

+0

위대한 .... 고마워. .. 내 인생과 시간을 많이 구해 줬어. –

관련 문제