选择器无法识别最后一行

发布于 2024-09-25 23:40:45 字数 2073 浏览 9 评论 0原文

这看起来像是直接来自文档的相当标准的代码,所以我不确定我做错了什么...

我的三组件选择器在加载时看起来不正确,选择指示器在选择器中放置得太低(因此灰色条放置正确,但清晰的凸起覆盖层太低)。

然后,当我选择一行时,除非我选择最后一行,否则一切都会完美运行,在这种情况下,它认为我选择了倒数第二行。

数组中的标题计数是正确的(例如 7),当我选择行“0-5”时,效果很好,但是当我选择行“6”(第 7 行)时,它仍然认为我正在选择“5”。因此第六行和第七行都将返回“5”而不是“5 和 6”。

这对于所有三个组件都会发生,并且实际上在代码中其他地方的另一个更简单的选择器中也以类似的方式发生(实际上,对于该选择器,只有当我来自父视图控制器时。当我来自子视图控制器就可以了)。

你能帮忙吗?

- (void)loadmypicker {
UIPickerView *mypicker = [[UIPickerView alloc] init];
[mypicker setDataSource:self];
[mypicker setDelegate:self];
pickerTitles_Dil = [[NSArray alloc] initWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", nil];
pickerTitles_Eff = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", nil];
pickerTitles_Stat = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", nil];
[mypicker setShowsSelectionIndicator:YES];
[mypicker selectRow:0 inComponent:0 animated:NO];
[mypicker selectRow:0 inComponent:1 animated:NO];
[mypicker selectRow:0 inComponent:2 animated:NO];
[mypicker setFrame:CGRectMake(0, 98, 320, 253)];
[self addSubview:mypicker];
    [mypicker release];
}

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (component == 0)
    return [pickerTitles_Dil objectAtIndex:row];
else if (component == 1)
    return [pickerTitles_Eff objectAtIndex:row];
else return [pickerTitles_Stat objectAtIndex:row];
 }

 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
 }


 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0)
{return [pickerTitles_Dil count];
}
else if (component == 1)
{return [pickerTitles_Eff count];
}
else 
{return [pickerTitles_Stat count];
}
   }

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog (@"row: %d, component :%d", row, component);
 }

感谢您的帮助!

还有其他人遇到过这个问题吗? 总而言之,选择器显示最后一行,但我无法选择它。谢谢!

This seems like pretty standard code straight from the doc, so I'm not sure what I am doing wrong...

My 3-component picker doesn't look right when it loads, with the selection indicator being placed too low across the picker (so the grey bar is correctly placed, but the clear raised-looking overlay is too low).

Then, when I select a row, it all works perfectly unless I select the last row, in which case it thinks I selected the next-to-the-last row.

The count of titles in the array is correct (7, for instance), and when I select rows "0-5" it works great, but when I select row "6" (the 7th row), it still thinks I am selecting "5". So both the sixth and the seventh rows will return "5" instead of "5 and 6".

This happens for all of the three components, and actually is happening in a similar way in another one of my simpler pickers elsewhere in the code (actually, with that picker, only when I come from the parent view controller. When I come from the child view controller it is fine).

Can you help?

- (void)loadmypicker {
UIPickerView *mypicker = [[UIPickerView alloc] init];
[mypicker setDataSource:self];
[mypicker setDelegate:self];
pickerTitles_Dil = [[NSArray alloc] initWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", nil];
pickerTitles_Eff = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", @"d", @"e", @"f", @"g", @"h", @"i", @"j", @"k", nil];
pickerTitles_Stat = [[NSArray alloc] initWithObjects:@"A", @"B", @"C", @"D", @"E", @"F", @"G", nil];
[mypicker setShowsSelectionIndicator:YES];
[mypicker selectRow:0 inComponent:0 animated:NO];
[mypicker selectRow:0 inComponent:1 animated:NO];
[mypicker selectRow:0 inComponent:2 animated:NO];
[mypicker setFrame:CGRectMake(0, 98, 320, 253)];
[self addSubview:mypicker];
    [mypicker release];
}

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (component == 0)
    return [pickerTitles_Dil objectAtIndex:row];
else if (component == 1)
    return [pickerTitles_Eff objectAtIndex:row];
else return [pickerTitles_Stat objectAtIndex:row];
 }

 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
 }


 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0)
{return [pickerTitles_Dil count];
}
else if (component == 1)
{return [pickerTitles_Eff count];
}
else 
{return [pickerTitles_Stat count];
}
   }

 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSLog (@"row: %d, component :%d", row, component);
 }

Thank you for your help!

Has anyone else had this problem?
To summarize, the picker shows the last row but I cannot select it. Thanks!

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

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

发布评论

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

评论(2

你在我安 2024-10-02 23:40:45

问题已修复:选择器高度不能超过216。因为我超过了253,所有其他问题都随之而来。谢谢!

Problem fixed: picker height can be no more than 216. Because I went over at 253, all the other problems followed. Thanks!

幼儿园老大 2024-10-02 23:40:45

不知何故,我认为以下数据源方法已被弃用,对吧?

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

如果您尝试使用以下方法,会发生什么情况?

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

也许这会有帮助?但我不确定这是否能解决你的问题。

编辑:尝试这样的方法,新方法不能直接返回字符串,但需要视图:

 /* Because the UIPickerView expects a UIView for every row you insert in the UIPickerView, you need to make one. What I do here is really simple. I create a UILabel* and with each row it requests I just change the text in the UILabel. */

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    /* Create a UILabel and set it 40 pixels to the right, to make sure it is put nicely in the UIPickerView */
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 280, 25)] autorelease];
        label.textColor = [UIColor blackColor];
        label.backgroundColor = [UIColor clearColor];

        switch (row)
        {
            default:
            case kUnderClock:
                label.text = @"Under clock";
                break;
            case kAboveSlider:
                label.text = @"Above lock slider";
                break;
            case kCenter:
                label.text = @"Vertically centered";
                break;
        }

        return label;
    }

抱歉缩进问题,直接从我的博客复制。

Somehow I think the following datasource method is deprecated, right?

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

What happens if you try to use the following method for that?

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

Maybe that will help? But I am not sure this will fix it for you.

EDIT: Try something like this, the new method cannot return strings directly, but needs views:

 /* Because the UIPickerView expects a UIView for every row you insert in the UIPickerView, you need to make one. What I do here is really simple. I create a UILabel* and with each row it requests I just change the text in the UILabel. */

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
    {
    /* Create a UILabel and set it 40 pixels to the right, to make sure it is put nicely in the UIPickerView */
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, 0, 280, 25)] autorelease];
        label.textColor = [UIColor blackColor];
        label.backgroundColor = [UIColor clearColor];

        switch (row)
        {
            default:
            case kUnderClock:
                label.text = @"Under clock";
                break;
            case kAboveSlider:
                label.text = @"Above lock slider";
                break;
            case kCenter:
                label.text = @"Vertically centered";
                break;
        }

        return label;
    }

Sorry for the indentation problem, copied this straight from my blog.

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