この質問は少し古いことは知っていますが、答えることで他の人を助けることができます.
あなたが抱えていたこの問題の解決策を見つけたと思います。
解決
- (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も使用します。
これが誰かに役立つことを願っています。