0

I have Picker with 3 components. My goal is create dependency the second component from first, and third from second. For example: when customer pick car maker AUDI in first component he will see list of models in second component: @"A3", @"A4", @"A5" etc. When he pick model @"A3" he will see list of engines for this certain model: @"1.8 TFSI", @"2.0 TFSI" etc. which list is be different for model @"A4": @"2.0 TFSI", @"2.0 TDI". So, certain model has its own list of engines. I did first part, when I choose car maker I've got list of models, but have no idea how handle another - make dependency between models and engines. I appreciate any help, thanks.

Here is my code:

MpgPresetDataViewController.h

@property (weak, nonatomic) IBOutlet UILabel *picCarLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

MpgPresetDataViewController.m Properties

@property (strong, nonatomic) NSMutableArray *make;
@property (strong, nonatomic) NSMutableArray *audi;
@property (strong, nonatomic) NSMutableArray *bmw;
@property (strong, nonatomic) NSMutableArray *cadillac;
@property (strong, nonatomic) NSMutableArray *tesla;
@property (strong, nonatomic) NSString *carMaker;
@property (strong, nonatomic) NSString *model;

View Did Load

- (void)viewDidLoad
{
[super viewDidLoad];    
_make = [[NSMutableArray alloc] initWithObjects:@"AUDI", @"BMW", @"CADILLAC", @"TESLA",
nil];  
_audi = [[NSMutableArray alloc] initWithObjects:@"A3", @"A4", @"A5", @"A6", @"A8",
@"Allroad", @"Q3", nil]; 
_bmw = [[NSMutableArray alloc] initWithObjects:@"1 series", @"2 series", @"3 series", @"4
series", @"5 series", @"7 series", nil];
_cadillac = [[NSMutableArray alloc] initWithObjects:@"ELR", @"CTS COUPE", @"ATS", @"CTS",
@"XTS", nil];    
_tesla = [[NSMutableArray alloc] initWithObjects:@"Model S", @"Model X", nil];

Number Of Components Method

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

Did Select Row Method

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

if (component == 0) {
    _carMaker = [[NSString alloc] initWithFormat:@"%@", [_make objectAtIndex:row]];  
}
[pickerView reloadComponent:1];   
}

Number Of Rows Method

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

 //Set numbers of rows
    if (component == 0) {
        return [_make count];
    }
    else {
        if ([_carMaker isEqualToString:@"AUDI"]) {
            return [_audi count];
        }    
        if ([_carMaker isEqualToString:@"BMW"]) {
            return [_bmw count];
        }    
        if ([_carMaker isEqualToString:@"CADILLAC"]) {
            return [_cadillac count];
        }
        if ([_carMaker isEqualToString:@"TESLA"]) {
            return [_tesla count];
        }            
    }
return 0;
}

Title for Row Method

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {   

if (component == 0) {
        return [_make objectAtIndex:row];
    }    
    else {
        if ([_carMaker isEqualToString:@"AUDI"]) {
            return [_audi objectAtIndex:row];
        }
        if ([_carMaker isEqualToString:@"BMW"]) {
            return [_bmw objectAtIndex:row];
        }
        if ([_carMaker isEqualToString:@"CADILLAC"]){
            return [_cadillac objectAtIndex:row];
        }
        else {
            return [_tesla objectAtIndex:row];
        }
    }
return 0;    
}
Sergey Z
  • 103
  • 11
  • Things would be MUCH simpler if you put all of the nested data (arrays of arrays of arrays) into a plist. Then reloading each component becomes easy. No hardcoding required based on string values. – rmaddy Aug 21 '14 at 22:21
  • Thanks, will research. – Sergey Z Aug 21 '14 at 22:24

0 Answers0