2010-09-29 81 views
1

这似乎是直接从文档非常标准的代码,所以我不知道我做错了......选择器不能识别的最后一排

我的三分量选择器看起来不正确时它加载时,选择指示器在拾取器上放置得太低(所以灰色条被正确放置,但明显的凸起的覆盖层太低)。

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

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

谢谢你的帮忙!

有没有其他人有这个问题?总之,选取器显示最后一行,但我不能选择它。谢谢!

+0

我可以建议你重新标记以包含语言/应用程序/ ???这是指 – Basic 2010-09-30 16:36:48

+0

当然......这有帮助吗?谢谢! – snorkelt 2010-09-30 18:32:07

回答

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

很抱歉的压痕问题,这个复制直接从我的博客。

+0

感谢您的回应!当我用viewForRow替换titleForRow行时,我得到一个[NSCFString superview]:无法识别的选择器发送到实例...我没有看到titleForRow在Apple Dev上被弃用,但是有没有办法让这个工作成功? – snorkelt 2010-10-02 01:19:50

+0

好吧,我改变了方法viewForRow(花了我一会儿,因为3个组件和很多标签),但它仍然没有选择最后一行(如果我的标签从0-6,我只能选择降至5)。此外,选择器的选择指示器太低,所以我认为问题在我的代码中的其他地方。所选的值是灰色的值,透明覆盖条低于此值。那可能是什么? – snorkelt 2010-10-02 15:31:08

+0

您是否尝试将UIPickerView放置在0x0位置,以查看它如何反应? – 2010-10-02 18:13:22

1

问题修正:拾取器高度不能超过216个。因为我在253时过去了,所有其他问题都随之而来。谢谢!

相关问题