UIPickerView 具有自定义视图和标签不滚动

发布于 2024-09-27 13:41:37 字数 1072 浏览 3 评论 0原文

我使用以下代码将标签和视图添加到 UIPickerView。

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


CustomPickerView *customView = [[CustomPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 180, 32)];

CustomPickerLabel *pickerLabelLeft = [[CustomPickerLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 80, 32)];
[pickerLabelLeft setTextAlignment:UITextAlignmentRight];
pickerLabelLeft.backgroundColor = [UIColor clearColor];
[pickerLabelLeft setText:@"1234"];

[customView addSubview:pickerLabelLeft];

return customView;

我使用视图的原因

是因为我想向该视图添加两个标签并将它们显示在选择器中。 CustomPickerView 和 CustomPickerLabel 类均包含以下代码:

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

代码对于显示和滚动效果很好,但是当我单击标签进行滚动时,它什么也不做。如果我在标签外部(如选择器的角落)单击,滚轮将按其应有的方式转向选择。

任何建议将不胜感激。

I'm using the following code to add a label and a view to a UIPickerView.

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


CustomPickerView *customView = [[CustomPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 180, 32)];

CustomPickerLabel *pickerLabelLeft = [[CustomPickerLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 80, 32)];
[pickerLabelLeft setTextAlignment:UITextAlignmentRight];
pickerLabelLeft.backgroundColor = [UIColor clearColor];
[pickerLabelLeft setText:@"1234"];

[customView addSubview:pickerLabelLeft];

return customView;

}

The reason I'm using a view is because I want to add two labels to this view and display them in the picker.
The CustomPickerView and CustomPickerLabel classes contain the following code, each:

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

the above code works fine for display and scrolling, but when I click on the label to scroll, it does nothing. If I click just outside the label, as in the corners of the picker, the wheel turns to the selection as it should.

Any suggestions would be appreciated.

Rod

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

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

发布评论

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

评论(1

留一抹残留的笑 2024-10-04 13:41:37

将您的 customView 的 userInteractionEnabled 属性设置为 NO。似乎如果将其设置为 YES,则自定义视图拦截触摸并且选择器无法滚动到点击的行。

Set your customView's userInteractionEnabled property to NO. It seems that if it is set to YES then custom view intercept touches and picker can't scroll to the tapped row.

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