UIPickerView点击滚动&自定义行UIViews

时间:2010-08-26 14:35:51

标签: iphone cocoa-touch uipickerview

您可以通过滑动和点击选择带下方或上方的项目来滚动UIPickerView。

您可以通过将NSString指定为标题或为每行指定可重复使用的UIView来填充UIPickerView。

我注意到的是,当我从提供字符串切换到提供视图时,点击滚动不再有效。这是预期的行为吗?我的观点应该将一些触摸事件转发给UIPickerView吗?

由于

编辑:

以下是我的实施中的代码示例:

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

// Row view creation 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;
}

2 个答案:

答案 0 :(得分:6)

为行视图设置userInteractionEnabled属性为NO必须解决您的问题。

答案 1 :(得分:0)

在PickerRowView类中,定义以下方法:

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

那应该摆脱亮点