UIPickerView 具有自定义视图和标签不滚动
我使用以下代码将标签和视图添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的 customView 的
userInteractionEnabled
属性设置为NO
。似乎如果将其设置为 YES,则自定义视图拦截触摸并且选择器无法滚动到点击的行。Set your customView's
userInteractionEnabled
property toNO
. It seems that if it is set to YES then custom view intercept touches and picker can't scroll to the tapped row.