UIPickerView 点击滚动和自定义行 UIViews
Posted
技术标签:
【中文标题】UIPickerView 点击滚动和自定义行 UIViews【英文标题】:UIPickerView tap to scroll & custom row UIViews 【发布时间】:2010-08-26 14:35:50 【问题描述】:您可以通过滑动和点击选择带下方或上方的项目来滚动 UIPickerView。
您可以通过将 NSString 指定为标题或为每一行指定可重用的 UIViews 来填充 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;
【问题讨论】:
【参考方案1】:将您的行视图的userInteractionEnabled
属性设置为NO
必须解决您的问题。
【讨论】:
【参考方案2】:在您的 PickerRowView 类中,定义以下方法:
- (void)didMoveToSuperview
if ([[self superview] respondsToSelector:@selector(setShowSelection:)])
[[self superview] performSelector:@selector(setShowSelection:) withObject:NO];
那应该去掉高光
【讨论】:
以上是关于UIPickerView 点击滚动和自定义行 UIViews的主要内容,如果未能解决你的问题,请参考以下文章
UIActionSheet 中的 UIPickerView:UI 冻结
单击 Mac OS X 扩展坞中的应用程序图标时的自定义行为