UIPickerView 点击滚动即可自定义行 UIView

发布于 2024-09-16 02:50:56 字数 1597 浏览 6 评论 0原文

您可以通过滑动和点击选择带下方或上方的项目来滚动 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;
}

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 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不美如何 2024-09-23 02:51:04

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

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

并且应该消除突出显示

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

撩人痒 2024-09-23 02:51:03

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文