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インスタンスをカスタム ビューとして使用する場合の 1 つの問題は、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のプロパティをUILabelに設定するYESと、強調表示の問題が修正さUIPickerViewれますが、タッチされた行を選択するための自動スクロールも無効になります。

ハイライト動作を無効にするが、 のデフォルトの自動スクロール機能を維持する場合は、 に含まれるインスタンスで関数UIPickerViewを呼び出します。これを行う方法は、次のようなクラスをサブクラス化することです。setShowSelectionUITableCellUIPickerViewUILabel

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

次に、以前にUILabelinのインスタンスを返していた場所にpickerView:viewForRow:forComponent:reusingView:、 のインスタンスを返しますPickerViewLabel例として、 Dougのコードを使用すると、' 'のすべてのケースを ' UILabel'に置き換えますPickerViewLabel。行を削除することを忘れないでpickerRowLabel.userInteractionEnabled = YES;ください。

于 2009-11-23T20:42:10.083 に答える
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 に答える
0

UIPickerView の「showsSelectionIndicator」プロパティを見たいと思うかもしれません

于 2009-06-06T19:26:42.697 に答える