选择器无法识别最后一行
这看起来像是直接来自文档的相当标准的代码,所以我不确定我做错了什么...
我的三组件选择器在加载时看起来不正确,选择指示器在选择器中放置得太低(因此灰色条放置正确,但清晰的凸起覆盖层太低)。
然后,当我选择一行时,除非我选择最后一行,否则一切都会完美运行,在这种情况下,它认为我选择了倒数第二行。
数组中的标题计数是正确的(例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题已修复:选择器高度不能超过216。因为我超过了253,所有其他问题都随之而来。谢谢!
Problem fixed: picker height can be no more than 216. Because I went over at 253, all the other problems followed. Thanks!
不知何故,我认为以下数据源方法已被弃用,对吧?
如果您尝试使用以下方法,会发生什么情况?
也许这会有帮助?但我不确定这是否能解决你的问题。
编辑:尝试这样的方法,新方法不能直接返回字符串,但需要视图:
抱歉缩进问题,直接从我的博客复制。
Somehow I think the following datasource method is deprecated, right?
What happens if you try to use the following method for that?
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:
Sorry for the indentation problem, copied this straight from my blog.