我在其中使用自定义UIPickerView
:
-(UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)view
用
UILabels
填充选择器。有没有一种方法可以禁用触摸时突出显示所选行的行为?我认为这是
UITableViewCell
中固有的基础UIPickerView
的属性,我找不到改变它的方法。 最佳答案
您需要确保自定义视图具有以下属性:
根据您的委托方法-pickerView:rowHeightForComponent:
和pickerView:widthForComponent:
,它的大小必须与UIPickerView期望的大小相同。如果未指定自定义高度,则默认高度为44。
背景颜色必须为[UIColor clearColor]
。
该视图必须捕获所有触摸。
使用UILabel
实例作为自定义视图时,一个陷阱是UILabel
将userInteractionEnabled
默认设置为NO
(另一方面,UIView
将该属性默认设置为YES
)。
根据这些要求,可以将Halle中的示例代码重写如下。此示例还正确地重用了先前创建的视图,这是快速滚动性能所必需的。
- (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;
}