0

我在pickerview中有两列。在第一列中有三个值:全部、水果和蔬菜。在第二列中有一些值:Apple、Carrot、Celery、Cherry、Pear、Tomato。

第一次我想在第一列中看到所有元素,在第二列中看到所有元素。但是当我在第一列中选​​择水果时,我想在第二列中看到苹果、樱桃和梨。可能吗?

非常感谢,特别是谁能发布一个例子。

4

1 回答 1

2

是的,这是可能的,作为您的示例,您只需执行以下步骤

1)制作具有多个数组的字典意味着这样

 mainArray =[ NSMutableArray new]; // for 1st column value
[mainArray addObject:@"All"];
[mainArray addObject:@"Fruites"];
[mainArray addObject:@"Vegetables"];

NSMutableArray *DummyArray = [[ NSMutableArray alloc]init];
[DummyArray addObject:@"Apple"];
[DummyArray addObject:@"Mango"];
[DummyArray addObject:@"Cherry"];
[DummyArray addObject:@"Table"];
[DummyArray addObject:@"Chair"];
[DummyArray addObject:@"Pear"];
[DummyArray addObject:@"Tomato"];


mainDict = [NSMutableDictionary new];
[mainDict setValue:DummyArray   forKey:@"All"];

NSMutableArray *DummyArrayF = [[ NSMutableArray alloc]init];
[DummyArrayF addObject:@"Apple"];
[DummyArrayF addObject:@"Mango"];
[DummyArrayF addObject:@"Cherry"];
[mainDict setValue:DummyArrayF   forKey:@"Fruites"];


NSMutableArray *DummyArrayV = [[ NSMutableArray alloc]init];
[DummyArrayV addObject:@"Pear"];
[DummyArrayV addObject:@"Tomato"];
[mainDict DummyArrayV   forKey:@"Vegetables"];

selectValue = @"All";

这里 'selectValue' 显示哪个

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

// 返回每个组件中的行数..

  - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
switch (component) {

    case 0:
        return 3;
        break;

        case 1:
        return [[mainDict valueForKey:selectValue] count];
        break;

     }
    return 0;
}

  - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
    case 0:
        return mainArray[row];
        break;

    case 1:
        return [mainDict valueForKey:selectValue][row];
        break;
    default:
        break;
}
return @"";
}

  - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{
 switch (component) {
    case 0:
        selectValue = mainArray[row];
        [pickerView1 reloadAllComponents];

        break;

    default:
        break;
}

 }

如果有任何问题随时问谢谢

于 2014-06-23T07:41:18.507 回答