在UIPickerView中覆盖突出显示的选择 [英] Overriding highlighted selection in UIPickerView
问题描述
我有一个自定义 UIPickerView
其中我使用:
I have a custom UIPickerView
where I use:
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
用 UILabels
填充选择器。有没有办法禁用在触摸时突出显示所选行的行为?
to populate the picker with UILabels
. Is there a way to disable the behavior of highlighting the selected row when touched?
我认为这是底层的属性 UITableViewCell $
UIPickerView
中固有的c $ c>,我找不到改变它的方法。
I think this is a property of the underlying UITableViewCell
inherent in the UIPickerView
and I can't find a way to change it.
推荐答案
您需要确保自定义视图具有以下属性:
You need to make sure your custom view has the following properties:
- 与UIPickerView基于您的委托方法所预期的相同维度 -
pickerView:rowHeightForComponent:
和pickerView:widthForComponent:
。 - 背景颜色必须
[UIColor clearColor]
。/ li>
- 视图必须捕获所有触摸。
- It needs to be sized to the same dimensions that the UIPickerView expects based on your delegate methods --
pickerView:rowHeightForComponent:
andpickerView:widthForComponent:
. The default height is 44 if you're not specifying a custom height. - The background color must be
[UIColor clearColor]
. - The view must capture all touches.
使用 UILabel
实例作为自定义视图是 UILabel
defaults userInteractionEnabled
到 NO
( UIView
,另一方面,默认此属性为 YES
) 。
The one gotcha when using UILabel
instances as the custom view is that UILabel
defaults userInteractionEnabled
to NO
(UIView
, on the other hand, defaults this property to YES
).
根据这些要求,来自 Halle 可以重写如下。此示例还正确重复使用先前创建的视图,这是快速滚动性能所需的。
Based on these requirements, the example code from Halle can be rewritten as follows. This example also correctly reuses previously created views, which is needed for fast scrolling performance.
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view {
UILabel *pickerRowLabel = (UILabel *)view;
if (pickerRowLabel == nil) {
// Rule 1: width and height match what the picker view expects.
// Change as needed.
CGRect frame = CGRectMake(0.0, 0.0, 320, 44);
pickerRowLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
// Rule 2: background color is clear. The view is positioned over
// the UIPickerView chrome.
pickerRowLabel.backgroundColor = [UIColor clearColor];
// Rule 3: view must capture all touches otherwise the cell will highlight,
// because the picker view uses a UITableView in its implementation.
pickerRowLabel.userInteractionEnabled = YES;
}
pickerRowLabel.text = [pickerDataArray objectAtIndex:row];
return pickerRowLabel;
}
这篇关于在UIPickerView中覆盖突出显示的选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!