Question

I'm trying to set two UIPickerViews, both are filled with NSDictionary, this work ok, but I need to the second UIPickerView implement a refresh depends of the selection of the first UIPickerView, understand me?

The first UIPickerView is filled this way:

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];
}

And the second UIPickerView is filled this way:

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];
}

You note variable idLocal, that corresponds to id of the first UIPickerView rows. Then, I need, if in the first UIPickerView is selected row with idLocal 1, in the second UIPickerView is reloaded or refreshing only with rows corresponds to idLocal 1.

Please hope somebody can help me, and sorry for my English :P

Was it helpful?

Solution

you have to implement <UIPickerViewDelegate> Method as below

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

and change your second UIPickerView fill Loop accordingly

OTHER TIPS

I would recommend using (NSInteger)component 0 is the first row and so on.

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)
    {

    }
}

This code works for just about all the picker view methods that provide the component NSInteger.

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


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

You will need to make sure you return the right item count for each component

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component == 0) {
    return wordArray.count;
}
else if (component == 1)
{
    return positionArray.count;
}
return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top