相对于在相同Picker的Component 0中进行的选择填充Picker视图的Component 1

时间:2012-11-05 05:36:37

标签: xcode uipickerview

我是XCode的新手。我正在使用Picker VIew并被困在一个地方。 我有一个带有两个组件的Picker View。我有三个阵列。 Array,Array1,Array2。

现在我想要完成的是。在组件0中选择Array的第一个对象时,第二个组件将由Array 1填充。 当从第一个组件中的Array中选择第二个对象时。第二个组件被重新加载并由Array2填充。

我对如何做到这一点非常困惑。

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

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
  {
return [Array count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row
        forComponent:(NSInteger)component
{
if (component == 0)
return [Array objectAtIndex:row];
else 
    return [[ArraysOFArrays objectAtIndex:row]objectAtIndex:0 ]; 
 //ArraysOFArrays = Array1 & Array2
[technologyPicker reloadComponent:1];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component
{   
    if ((component == 0) && (row ==0))
    { 
    if (component == 1)
    {
        [Array1 objectAtIndex:0];

    }
    selectTechnology.text = [technologyData objectAtIndex:row];
   [technologyPicker reloadComponent:1]; 
}

if ((component == 0) && (row ==1))
{ 
    if (component == 1)
    {
    [Array2 objectAtIndex:0];

    }

    [technologyPicker reloadComponent:1]; 
}


}

1 个答案:

答案 0 :(得分:1)

我修改了你的代码试试这个。获取int类型的全局变量“selected”并将其值初始化为0。 BTW什么是“技术数据”?

没有组件

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

组件中的行数

- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component
  {
     if (component==0)
         return [Array count];
     else
     {
          if(selected==0)
              [[ArraysOFArrays objectAtIndex:0]count];
          else
              [[ArraysOFArrays objectAtIndex:1]count];   
     }
}

行标题

- (NSString *)pickerView:(UIPickerView *)pickerView
         titleForRow:(NSInteger)row
        forComponent:(NSInteger)component
{
if (component == 0)
    return [Array objectAtIndex:row];
else 
    return [[ArraysOFArrays objectAtIndex:selected]objectAtIndex:row]; 
 //ArraysOFArrays = Array1 & Array2
}

确实在组件中选择行

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
  inComponent:(NSInteger)component
{   
    if (component == 0)
    { 
        if (row == 0)
        {
            selected=0;
            [technologyPicker reloadComponent:1];
        }
        else
        {
             selected=1;
            [technologyPicker reloadComponent:1];
        }
     }
     else
        **selectTechnology.text = [[ArraysOFArrays objectAtIndex:selected]objectAtIndex:row];**

}