开发者

UIPickerView tap to scroll & custom row UIViews

You can scroll an UIPickerView both by swiping and by tapping items under or above the selection belt.

You can populate an UIPickerView by either specifying NSString as titles or reusable UIViews for each row.

What I've noticed is that when I switched from providing strings to providing view, tap to scroll no longer worked. Is this expected behavior. Should my views forward some touch events to the UIPickerView?

Thanks

EDIT:

Here's a code sample from my implementation:

// Creating the picker
screenPicker_ = [UIPickerView new];
screenPicker_.showsSelectionIndicator = YES;
screenPicker_.delegate = delegate_;
screenPicker_.dataSource = delegate_;
[self addSubview:screenPicker_];
[screenPicker_ release];

// Row view creati开发者_JAVA百科on delegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    PickerRowView *pickerRowView = (PickerRowView*)view;
    if(view == nil)
    {
        pickerRowView = [[PickerRowView alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, PICKER_ROW_HEIGHT)];
    }

    [pickerRowView SetTitle:@"some title"];

    return pickerRowView;
}

// initailizer of the PickerRowView class (an UIView subclass)
- (id)initWithFrame:(CGRect)frame 
{
    if ((self = [super initWithFrame:frame])) 
    {
        CGFloat titleHeight = frame.size.height * CONTENT_TO_FRAME_RATIO;
        title_ = [[UILabel alloc] initWithFrame:CGRectMake(TITLE_X, (frame.size.height - titleHeight) / 2, frame.size.width, titleHeight)];
        [title_ setFont:[UIFont fontWithName:@"StainlessExt-Light" size:titleHeight]];
        title_.backgroundColor = [UIColor clearColor];
        [self addSubview:title_];
        [title_ release];

        self.userInteractionEnabled = NO;
    }
    return self;
}


Setting userInteractionEnabled property to NO for your row views must solve your problem.


In your PickerRowView class, define the following method:

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

and that should get rid of the highlight

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜