8

我有一个使用习惯UIPickerView

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

用 填充选择器UILabels。有没有办法禁用触摸时突出显示所选行的行为?

我认为这是底层UITableViewCell固有的属性,UIPickerView我找不到改变它的方法。

4

4 回答 4

16

您需要确保您的自定义视图具有以下属性:

  1. 它需要根据您的委托方法调整为 UIPickerView 期望的相同尺寸 -pickerView:rowHeightForComponent:pickerView:widthForComponent:. 如果您未指定自定义高度,则默认高度为 44。
  2. 背景颜色必须是[UIColor clearColor].
  3. 视图必须捕获所有触摸。

使用UILabel实例作为自定义视图时的一个问题是UILabel默认userInteractionEnabledNO( 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;
}
于 2009-09-16T16:00:22.323 回答
12

userInteractionEnabled属性设置UILabelYES修复突出显示问题,但它也禁用UIPickerView自动滚动以选择已触摸的行。

如果要禁用突出显示行为,但要保留UIPickerView的默认自动滚动功能,setShowSelectionUITableCellUIPickerView. 一种方法是对类进行子UILabel类化,类似于以下内容:

PickerViewLabel.h -

#import <UIKit/UIKit.h>

@interface PickerViewLabel:UILabel 
{
}

@end

PickerViewLabel.m -

#import "PickerViewLabel.h"

@implementation PickerViewLabel

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

@end

然后,在您之前返回 in 的实例的地方UILabelpickerView:viewForRow:forComponent:reusingView:返回 的实例PickerViewLabel。例如,使用DougUILabel中的代码,您可以用 ' '替换所有 ' ' 的情况PickerViewLabel。只记得删除pickerRowLabel.userInteractionEnabled = YES;线。

于 2009-11-23T20:42:10.083 回答
0

我想你可能想看看 UIPickerView 的“showsSelectionIndicator”属性

于 2009-06-06T19:26:42.697 回答
0

我不确定是否有一种简单的方法可以删除选择反馈,但是如果将标签的背景设为白色并将其调整为与蓝色选择矩形相同的尺寸,则可以将其覆盖:

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

    UILabel *pickerRowLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 316, 40)];
    pickerRowLabel.backgroundColor = [UIColor whiteColor];
    pickerRowLabel.text = [pickerDataArray objectAtIndex:row];  
    [self.view addSubview:pickerRowLabel];

    return pickerRowLabel;

}

宽度为 316 时,标签覆盖了除每边的蓝色条之外的所有内容,在 320 处,它完全覆盖了选择反馈,但它也开始覆盖了一些外轮渐变,这可能会或可能不会打扰您。

于 2009-06-10T17:29:11.233 回答