我試圖爲我的問題找到一個解決方案一個多星期了,但沒有成功解決這個網站上的其他類似問題。如何從第一個組件選擇中加載UIPickerView的第二個組件中的數據
我有我的pickerView多個NSMutableArrays,我試圖根據第一個組件的選擇設置第二個組件的數據。我的主要問題是,當我選擇第一個組件中的第一個選項以外的任何其他組件時,第二個組件的數據是空的。當選擇第一個組件的第一個選項時,我的所有數組都依次排列在第二個組件中,但每個組件數據只需要一個數組。
這是我在ViewController.h代碼:
@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
{
NSMutableArray *maListeTrier;
NSMutableArray *maListeTypes;
NSMutableArray *maListeSousTypes;
NSMutableArray *maListeSousTypes2;
NSMutableArray *maListeSousTypes3;
NSMutableArray *maListeSousTypes4;
NSMutableArray *maListeSousTypes5;
}
@property NSString *selectedType;
@property NSString *selectedArray;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *labelType;
@property (weak, nonatomic) IBOutlet UILabel *labelSousType;
這是我ViewController.m代碼:
@synthesize selectedType;
@synthesize selectedArray;
- (void)viewDidLoad {
[super viewDidLoad];
selectedType=[[NSString alloc]init];
selectedArray=[[NSString alloc]init];
maListeTypes = [[NSMutableArray alloc] init];
[maListeTypes addObject:@"Type 1"];
[maListeTypes addObject:@"Type 2"];
maListeSousTypes=[[NSMutableArray alloc]init];
[maListeSousTypes addObject:@"Sous-type 1"];
[maListeSousTypes addObject:@"Sous-type 2"];
[maListeSousTypes addObject:@"Sous-type 3"];
[maListeSousTypes addObject:@"Sous-type 4"];
[maListeSousTypes addObject:@"Sous-type 5"];
maListeSousTypes2=[[NSMutableArray alloc]init];
[maListeSousTypes addObject:@"22222"];
[maListeSousTypes addObject:@"22222"];
[maListeSousTypes addObject:@"22222"];
[maListeSousTypes addObject:@"22222"];
[maListeSousTypes addObject:@"22222"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Méthode pour déterminer le nombre de colonnes dans chaque picker view
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
//Méthode pour déterminer le nombre de rangées dans chaque picker view
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if(component==0){
return [maListeTypes count];
}
else if(component==1){
if([selectedType isEqualToString:@"Type 1"]){
return [maListeSousTypes count];
}
else if([selectedType isEqualToString:@"Type 2"]){
return [maListeSousTypes2 count];
}
else{
return 0;
}
}
else{
return 0;
}
}
//Méthode pour indiquer quoi afficher comme choix dans chaque picker view
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if(component==0){
return [maListeTypes objectAtIndex:row];
}
else{
if([selectedType isEqualToString:@"Type 1"]){
return [maListeSousTypes objectAtIndex:row];
}
else{
return [maListeSousTypes2 objectAtIndex:row];
}
}
}
//Méthode pour indiquer quelle action va se produire lorsqu'on clique sur un des choix des picker views
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component==0){
selectedType=[maListeTypes objectAtIndex:row];
[pickerView reloadAllComponents];
_labelType.text=[maListeTypes objectAtIndex:[pickerView selectedRowInComponent:0]];
}
else if(component==1){
_labelSousType.text=[maListeSousTypes objectAtIndex:[pickerView selectedRowInComponent:1]];
}
}
當我的第一個組件選擇是首選「1型」在我的第二個組成部分我看到的NSMutableArray數據‘maListeSousTypes’和‘maListeSousTypes2’此起彼伏喜歡這裏: Screenshot
當從第一個組件中選擇「類型2」時,我的第二個組件是空的。
你永遠不會向maListeSousTypes2添加任何東西。你錯過了我懷疑的複製和粘貼錯誤。 '[maListeSousTypes addObject:@「22222」];'只是添加到'maListeSousTypes'中,您已經在'NSString's之前添加了一堆。 – beyowulf
非常感謝!這是我錯過了,並沒有看我的代碼的正確部分。現在一切正常。 – Vomarhk