如何在级联中设置UIPickerView

时间:2014-03-20 15:41:34

标签: ios view uipickerview picker

我正在尝试设置两个UIPickerView,两个都填充NSDictionary,这项工作正常,但我需要第二个UIPickerView实现刷新取决于选择第一个UIPickerView,了解我?

第一个UIPickerView以这种方式填充:

for (NSDictionary *local in json) {
    NSString *nombre = [local objectForKey:@"name"];
    NSString *idLocal = [local objectForKey:@"id"];

    self.dicLocales = [NSDictionary dictionaryWithObjectsAndKeys: idLocal, @"idLocal", nombre, @"nombreLocal", nil];
    [listaLocales addObject:self.dicLocales];
}

第二个UIPickerView以这种方式填充:

for (NSDictionary *servicio in json) {
    NSString *nombre = [servicio objectForKey:@"name"];
    NSString *idServicio = [servicio objectForKey:@"id"];

    self.dicServicios = [NSDictionary dictionaryWithObjectsAndKeys: idServicio, @"idServicio", nombre, @"nombreServicio", idLocal, @"idLocal", nil];
    [listaServicios1 addObject:self.dicServicios];
}

您注意到变量idLocal,它对应于第一行UIPickerView行的ID。然后,我需要,如果在第一个UIPickerView中选择了具有idLocal 1的行,则在第二个UIPickerView中重新加载或刷新只有行对应于idLocal 1。

请希望有人能帮助我,对不起我的英语:P

2 个答案:

答案 0 :(得分:1)

您必须实施<UIPickerViewDelegate>方法,如下所示

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if ([pickerView isEqual:FirstPickerView]) {
        //SecondPickerView fill code
        [SecondPickerView reloadAllComponents];
    }
    else {
        //SecondPickerView Selected row
    }
}

并相应更改您的第二个UIPickerView 填充循环

答案 1 :(得分:0)

我建议使用(NSInteger)组件0是第一行,依此类推。

if (component == 0) {
    if (row == 0) {

    }
    else if (row == 1)
    {

    }
}
else if (component == 1)
{
    selectedPositionIndex = row;
    if (row == 0) {

    }
    else if (row == 1)
    {

    }
    else if (row == 2)
    {

    }
    else if (row == 3)
    {

    }
}

此代码适用于提供组件NSInteger的所有选择器视图方法。

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view


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

您需要确保为每个组件返回正确的项目数

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
    return wordArray.count;
}
else if (component == 1)
{
    return positionArray.count;
}
return 0;
}