1

このUIPickerViewクラスにはselectedRowInComponent、コンポーネントに対して選択された行を返すメソッドがあります。これは、ピッカービューの中央にあるアイテムである場合とそうでない場合があります。私が欲しいのは、pickerViewデータのどのインデックスが現在表示されているか(選択されていないか)を知ることができるようにすることです。

違いを説明するために、UIPickerViewダイヤルで値をタップすると、その値の行は常にで返されるように見えますselectedRowInComponent。ただし、ユーザーがピッカーをいくつかの値でスクロールしてから指をselectedRowInComponent離すと、更新されないか、スクロールしたばかりの値に更新されます。

ピッカーコンポーネントが実際にどこにダイヤルされているかを判断する方法はありますか?

値はメソッドで常に使用可能ですが、メソッドpickerView:didSelectRow:inComponent:でクエリしたいのですがpickerView:titleForRow:forComponent:

なんで?UIPickerViewがスクロールされている間、呼び出されてselectedRowInComponentいないためです。何らかの理由でデータの最後に到達し、さらに追加する必要がある場合は、呼び出されているメソッドでそれを行う必要があります。pickerView:titleForRow:forComponent:これは、すべての行に対して呼び出されるように見えます。

4

1 に答える 1

0

この質問は少し古いことは知っていますが、答えることで他の人を助けることができます.

あなたが抱えていたこの問題の解決策を見つけたと思います。


解決

- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row
        forComponent:(NSInteger)component
{
    // Passing the row being viewed to a function that does something with it.
    [self handleRowBeingViewed:[pickerView selectedRowInComponent:component]];
}

- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component
{
    // Passing the row being viewed to a function that does something with it.
    [self handleRowBeingViewed:[pickerView selectedRowInComponent:component]];
}

/**
 * It receives, in input 'rowBeingViewed', the row being viewed by the user in the center of the picker view, and use it to XYZW... 
 *
 *
 * @param rowBeingViewed     Row currently being viewed by the user in the center of the picker view.
 * @return
 */
- (void) handleRowBeingViewed:(NSInteger)rowBeingViewed
{
     // Printing for debugging.
     NSLog(@"String Being Viewed: %@", pickerViewArray[rowBeingViewed]);

     // DO YOUR STUFF HERE
     // ...
}

説明

UIPickerViewを使用すると、selectedRowInComponentメソッドを使用して、いつでもどの行が選択されているか (または中央に表示されているかは同じだと思います) を知ることができます。

デリゲートのメソッドpickerView:titleForRow:forComponent内でそのメソッドselectedRowInComponentを使用すると、新しいタイトルがピッカー ビューでユーザーに「印刷」されるたびに、どの行が中央に表示されているかを知ることができます。

ピッカー ビューで 1 行または 2 行だけスクロールすると、デリゲートのメソッドpickerView:titleForRow:forComponentが呼び出されず、どの行が中央に表示されているかがわからないことがあります。これを解決するには、デリゲートのメソッドpickerView:didSelectRow:inComponent:内で同じメソッドselectedRowInComponentも使用します。

これが誰かに役立つことを願っています。

于 2016-06-07T19:41:51.240 に答える