2

我是 XCode 的新手。我正在使用 Picker VIew 并卡在一个地方。我有一个带有两个组件的选取器视图。我有三个数组。数组,数组 1,数组 2。

现在我想要完成的是。当在组件 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]; 
}


}
4

1 回答 1

1

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

组件数量

- (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];**

}
于 2012-11-05T07:07:21.157 回答