0

私たちのデザイナーは、UITableView の右側にあるインデックスを UITableView 自体よりも高くするという優れたデザインを考え出しました。また、検索用のカスタム画像と、デフォルトの実装よりも優れたフォント/色もあります。ただし、 UITableView はインデックスのカスタマイズをサポートしていないため (私が知っている)、これを手動で実行しようとしています。UI は次のようになります。

ここに画像の説明を入力

これは簡単な作業のはずですが、私はそれを機能させるのに非常に苦労しています。テーブルの右側に UIView を設定し、インデックスを表すために UIButtons を上から下に並べました。目標は、ユーザーが UIButtons をドラッグイン/アウトするときに、UITableView を正しいセクションにジャンプすることです。

私は周りを検索しましたが、これを行うには、UIControlEventTouchDragOutside および UIControlEventTouchDragEnter イベントをリッスンして、UIButtons のいずれかに出入りしたときを知ることになるようです。

この目的のために、ボタンのリスト全体を IBOutletCollection として設定し、ViewDidLoad で次のように初期化しました。

@property (retain, nonatomic) IBOutletCollection(UIButton) NSArray* indexButtons;


@implementation ViewBeerListViewController
...
@synthesize indexButtons = _indexButtons;

- (void)viewDidLoad
{
    [super viewDidLoad];

    ...

    // Go through the index buttons and set the change function
    for (int i = 0; i < [self.indexButtons count]; ++i)
    {
        UIButton* button = [self.indexButtons objectAtIndex:i];
        [button addTarget:self action:@selector(touchDragOutside:) forControlEvents:UIControlEventTouchDragOutside];
        [button addTarget:self action:@selector(touchDragEnter:) forControlEvents:UIControlEventTouchDragEnter];
    }
}

The functions touchDragOutside and touchDragEnter look like this:

- (IBAction)touchDragOutside:(UIButton*)sender
{
    NSLog(@"Button %@ touch dragged outside", [sender titleLabel].text);
}

- (IBAction)touchDragEnter:(UIButton*)sender
{
    NSLog(@"Button %@ touch dragged enter", [sender titleLabel].text);
}

これはすべてビルドして実行します。ただし、タッチを開始したボタンのイベントしか取得できないようです。たとえば、「G」の文字をタッチダウンして上下にドラッグし始めると、「G」から「G」へのタッチ ドラッグのログのみが表示されます。それらを調べても、他の UIButtons のイベントは発生しません。

この問題を解決するための助けをいただければ幸いです。私は、非常に些細な問題のように思えることに、非常に長い間悩まされてきました。

ありがとう!

4

1 に答える 1

1

UIButtons で作成する代わりに、各文字の UILabels を含むカスタム UIView を作成してみてください。次に、カスタム UIView クラスで、次をオーバーライドします。

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:

それらを使用して、どのラベルが触れられているかを判断します。たとえば、touchesMoved 内では、次のようにすることができます。

UITouch * touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
for(UIView * indexView in self.indexViews) {
    if(CGRectContainsPoint(indexView.frame,point)) {
        //indexView was touched. do something here
        break;
    }
}

UILabels の代わりに一番上のものに UIImageView を使用することもでき、これは引き続き機能することに注意してください。

于 2013-01-15T18:18:10.293 に答える