当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - How to detect UIPickerView central row when spinning

问题描述:

Because of lack of screen space, i try to display description (UILabel appearing right to pickerView when spinning) of image in UIPickerView when user spins the wheel. I use viewForRow: to detect the spin and to show the description. The problem is, using the code i've written so far, it can't display the description of first 2 rows and last 2 rows.

Here is my code:

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

{

UIImageView *image=[[[UIImageView alloc]initWithImage:[UIImage imageNamed:[zodiacSigns objectAtIndex:row]]]autorelease];

selectedZodiac.frame=CGRectMake(80, 80, 50, 50);

selectedZodiac.hidden=NO;

@try {

//Here -2 is offset of the view in central row

selectedZodiac.text=[zodiacSigns objectAtIndex:row-2];

}

@catch (NSException *exception) {

NSLog(@"Exception caught");

}

@finally {

}

if([yourZodiacPicker selectedRowInComponent:0]!=0)

{

[self.view insertSubview:selectedZodiac aboveSubview:self.view];

}

NSLog(@"reuseView");

return image;

}

Any help would be greatly appreciated!

网友答案:

I guess the best way would to recreate the PickerView with a ScrollView/TableView and a matching overlay image if you really need this functionality.

Disable selection/highlight and scroll to the nearest "cell" when the scrollview stops.

Then determinate the cell that is in the middle of the screen. You should also add a view before and after the content, so the user can move the first/last cell to the middle of the screen.

网友答案:

I might have misunderstood, but if what you want is to display a description of the image currently selected, you should implement pickerView:didSelectRow:inComponent: and set the description label there, instead of doing it in viewForRow:forComponent:. It would be something like this:

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    selectedZodiac.frame=CGRectMake(80, 80, 50, 50);
    selectedZodiac.hidden=NO;
    selectedZodiac.text=[zodiacSigns objectAtIndex:row];
}

This way you would always be displaying the description of the selected picker row.

分享给朋友:
您可能感兴趣的文章:
随机阅读: