我有3个组件的Picker。我的目标是从第一个创建依赖第二个组件,从第二个创建第三个组件。例如:当客户选择第一个组件中的汽车制造商AUDI时,他将看到第二个组件中的模型列表:@“A3”,@“A4”,@“A5”等。当他选择模型@“A3”时,他将看到列表这种特定型号的发动机:@“1.8 TFSI”,@“2.0 TFSI”等,型号@“A4”的列表不同:@“2.0 TFSI”,@“2.0 TDI”。因此,某些型号有自己的引擎列表。 我做了第一部分,当我选择汽车制造商我有模型列表,但不知道如何处理另一个 - 模型和引擎之间的依赖。 感谢您的帮助,谢谢。
这是我的代码:
MpgPresetDataViewController.h
@property (weak, nonatomic) IBOutlet UILabel *picCarLabel;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
MpgPresetDataViewController.m属性
@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;
查看已加载
- (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];
组件数量方法
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
选择行方法
-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:
(NSInteger)component {
if (component == 0) {
_carMaker = [[NSString alloc] initWithFormat:@"%@", [_make objectAtIndex:row]];
}
[pickerView reloadComponent:1];
}
行数方法
-(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;
}
行方法的标题
-(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;
}