选择器视图在pickerview中显示数字但在标签上没有正确显示?

时间:2009-10-11 08:41:14

标签: objective-c iphone-sdk-3.0 uipickerview

我已经从界面builder.i添加了一个pickerview,我使用这个方法显示这样的数据......

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (pickerView == SpotPickerView) // don't show selection for the custom picker
{
  // report the selection to the UI label
 label.text = [NSString stringWithFormat:@"%d - %@ - %d",[pickerView selectedRowInComponent:0],
   [RowArray objectAtIndex:[pickerView selectedRowInComponent:1]],
   [pickerView selectedRowInComponent:2]];
}
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
 NSString *returnStr = @"";
 if (pickerView == SpotPickerView)
 {
  if (component == 0)
  {
    returnStr = [[NSNumber numberWithInt:row+1] stringValue];
  }
  else if(component==1)
  {
    returnStr = [RowArray objectAtIndex:row];
  }
else
 {
   returnStr =[[NSNumber numberWithInt:row+1] stringValue];
  }
}

  return returnStr;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
  CGFloat componentWidth = 0.0;

  if (component == 0)
  componentWidth = 90.0;
  else if (component == 1)
  componentWidth = 100.0; 
  else 
  componentWidth = 90.0; 

  return componentWidth;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
 {
   return 40.0;
 }

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
   int count;
   if (component == 0)
   count = 15;
  else if (component == 1)
   count=[RowArray count]; 
 else 
  count = 100;

 return count;
}

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

这里是RowArray

- (void)viewDidLoad {
RowArray = [[NSArray arrayWithObjects:
  @"A", @"B", @"C",
  @"D", @"E", @"F", @"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T",
  @"U",@"V",@"W",@"X",@"Y",@"Z",
  nil] retain];
}

但是选择第一行显示了这一点。

alt text

我需要将值存储在数据库中,因此我需要1而不是零。

如何重置我的选择器view.means当我点击一个按钮时它会自动进入每列的第0个索引。

1 个答案:

答案 0 :(得分:1)

label.text = [NSString stringWithFormat:@"%d - %@ - %d",[pickerView selectedRowInComponent:0],
    [RowArray objectAtIndex:[pickerView selectedRowInComponent:1]],
    [pickerView selectedRowInComponent:2]];

此代码检索组件中选定行的行号。数组是零索引的,pickerview(可能)使用数组来管理行。因此,第一行将为0,第二行为1,第三行为2,依此类推。这可以通过以下方式轻松解决:

[pickerView selectedRowInComponent:0] + 1

您可以使用selectRow:inComponent:animated以编程方式选择某些行如果您希望在每列中选择第0个索引,只需对每个组件执行:[pickerView selectRow:0 inComponent:0 animated:YES]