UIPickerView 旋转在 iOS 5 中不会停止

发布于 2025-01-01 14:45:05 字数 2107 浏览 2 评论 0原文

我在使用 UIPickerView 时遇到一些问题。它的委托方法:

-(void)pickerView:(UIPickerView *)pickerView
     didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component;

当用户点击所选行下方的行时不会被调用。

选择器动画并将“触摸”行放置在选择器中间,但它的旋转不会停止(它继续向上移动约 1px 向下移动 1px),并且不会调用委托方法。

有没有人遇到同样的问题并有解决方案?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow1:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
CGFloat pickerRowRectHeight = PICKER_ROW_HEIGHT;

if (!view) 
{
    CGFloat rectWidth = 300;

    CGRect viewRect = !component ? CGRectMake(0, 0, rectWidth, pickerRowRectHeight) : CGRectMake(0, 0, 50, pickerRowRectHeight);
    view = [[[UIView alloc] initWithFrame:viewRect] autorelease];
    view.backgroundColor = [UIColor clearColor];

    CGRect labelRect = CGRectMake(32 , 0, rectWidth-12, pickerRowRectHeight);
    CGFloat fontSize = FONT_SIZE;

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(10, 7, 16, 16);
    imageView.backgroundColor = [ UIColor clearColor];
    imageView.tag = IMAGE_TAG;
    [view addSubview:imageView];
    [imageView release];

    UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
    label.lineBreakMode = UILineBreakModeMiddleTruncation;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:fontSize];
    label.tag = LABEL_TAG;          
    [view addSubview:label];
    [label release];
} 



UILabel *label = (UILabel *)[view viewWithTag:LABEL_TAG];
label.text = [[someArray objectAtIndex:row] objectForKey:@"title"];
UIImageView *leftImg = (UIImageView*) [view viewWithTag:IMAGE_TAG];
NSData *dataUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[someArray objectAtIndex:row] objectForKey:@"icon"]]];
if (dataUrl!=nil) {
    leftImg.image = [UIImage imageWithData: dataUrl];
}
else
{
    leftImg.image = [UIImage imageNamed:[[someArray objectAtIndex:row] objectForKey:@"icon"]];
}


view.userInteractionEnabled = NO;

return view;}

I'm having some problems with an UIPickerView. Its delegate method :

-(void)pickerView:(UIPickerView *)pickerView
     didSelectRow:(NSInteger)row
      inComponent:(NSInteger)component;

is not called when the user taps on the row below the selected one.

The picker animate and place the "touched" row in the middle of the picker but it's spinning doesn't stops(it continue to move about 1px up and 1px down) and the delegate method is not called.

Is there anyone who had the same problem and has a solution for this?

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow1:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
CGFloat pickerRowRectHeight = PICKER_ROW_HEIGHT;

if (!view) 
{
    CGFloat rectWidth = 300;

    CGRect viewRect = !component ? CGRectMake(0, 0, rectWidth, pickerRowRectHeight) : CGRectMake(0, 0, 50, pickerRowRectHeight);
    view = [[[UIView alloc] initWithFrame:viewRect] autorelease];
    view.backgroundColor = [UIColor clearColor];

    CGRect labelRect = CGRectMake(32 , 0, rectWidth-12, pickerRowRectHeight);
    CGFloat fontSize = FONT_SIZE;

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(10, 7, 16, 16);
    imageView.backgroundColor = [ UIColor clearColor];
    imageView.tag = IMAGE_TAG;
    [view addSubview:imageView];
    [imageView release];

    UILabel *label = [[UILabel alloc] initWithFrame:labelRect];
    label.lineBreakMode = UILineBreakModeMiddleTruncation;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont systemFontOfSize:fontSize];
    label.tag = LABEL_TAG;          
    [view addSubview:label];
    [label release];
} 



UILabel *label = (UILabel *)[view viewWithTag:LABEL_TAG];
label.text = [[someArray objectAtIndex:row] objectForKey:@"title"];
UIImageView *leftImg = (UIImageView*) [view viewWithTag:IMAGE_TAG];
NSData *dataUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[someArray objectAtIndex:row] objectForKey:@"icon"]]];
if (dataUrl!=nil) {
    leftImg.image = [UIImage imageWithData: dataUrl];
}
else
{
    leftImg.image = [UIImage imageNamed:[[someArray objectAtIndex:row] objectForKey:@"icon"]];
}


view.userInteractionEnabled = NO;

return view;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2025-01-08 14:45:05

检查您是否分配了选择器视图委托,

- (void)viewDidLoad
{

pickerView.delegate = self;
...

Check that you assigned picker view delegate,

- (void)viewDidLoad
{

pickerView.delegate = self;
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文