Picker无法识别最后一行

时间:2010-09-29 01:55:13

标签: iphone objective-c components row uipickerview

这似乎是直接来自doc的标准代码,所以我不确定我做错了什么......

我的3分量拾取器在加载时看起来不正确,选择指示器在拾取器上放置得太低(因此灰色条正确放置,但透明的凸起覆盖层太低)。 / p>

然后,当我选择一行时,除非我选择最后一行,否则它完全有效,在这种情况下,它认为我选择了倒数第二行。

数组中的标题数是正确的(例如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);
 }

感谢您的帮助!

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

2 个答案:

答案 0 :(得分:1)

问题已解决:选择器高度不能超过216.因为我在253处结束,所以其他问题都出现了。谢谢!

答案 1 :(得分:0)

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

- (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;
    }

对于缩进问题感到抱歉,请直接从我的博客中复制。