如何从第一个组件选择中将数据加载到 UIPickerView 的第二个组件中
How to load data in second component of UIPickerView from first component selection
我已经尝试为我的问题找到一个多星期的解决方案,但在解决此网站上的其他类似问题时都没有成功。
我的 pickerView 有多个 NSMutableArray,我正在尝试根据第一个组件的 selection 设置第二个组件的数据。我的主要问题是,当我 select 除了第一个组件中的第一个选择之外的任何其他内容时,第二个组件的数据为空。当第一个组件中的第一个选择是 selected 时,我在第二个组件中将所有数组一个接一个地放在一起,但我只希望每个组件数据有一个数组。
这是我在 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]];
}
}
当我的第一个组件 selection 是首选 "Type 1" 时,在我的第二个组件中,我看到了来自 NSMutableArray "maListeSousTypes" 和 "maListeSousTypes2" 的数据,就像这里一样:
Screenshot
当 "Type 2" 从第一个组件 select 编辑时,我的第二个组件是空的。
这必须比您正在做的更简单。让我通过一个简单的例子向您展示我是如何做到的:
@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *leftComponentItems; // Items from this array will populate the left component
@property (nonatomic, strong) NSArray *rightComponentItemsA; // Items from this array will populate the right component, when the 1st row of the left component is selected
@property (nonatomic, strong) NSArray *rightComponentItemsB; // Items from this array will populate the right component, when the 2nd row of the left component is selected
@property (nonatomic, assign) NSInteger selectedLeftRow; // This keeps track of the selected row on the left component (you could take that from the datasource as well)
@end
然后在您的实施中:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedLeftRow = 0;
self.leftComponentItems = @[@"first", @"second"];
self.rightComponentItemsA = @[@"A1", @"A2", @"A3", @"A4", @"A5"];
self.rightComponentItemsB = @[@"B1", @"B2", @"B3", @"B4", @"B5", @"B6", @"B7"];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2; // We have left & right components, this will always be "2"
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return self.leftComponentItems.count;
}
if (component == 1) {
if (self.selectedLeftRow == 0) {
return self.rightComponentItemsA.count;
} else {
return self.rightComponentItemsB.count;
}
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return self.leftComponentItems[row];
}
if (component == 1) {
if (self.selectedLeftRow == 0) {
return self.rightComponentItemsA[row];
} else {
return self.rightComponentItemsB[row];
}
}
return @"default";
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
self.selectedLeftRow = row; // We keep track of the selected row
[pickerView reloadComponent:1]; // When we select a row in the left component, update the data on the right component
}
}
@end
有什么不明白的地方尽管问。
我已经尝试为我的问题找到一个多星期的解决方案,但在解决此网站上的其他类似问题时都没有成功。
我的 pickerView 有多个 NSMutableArray,我正在尝试根据第一个组件的 selection 设置第二个组件的数据。我的主要问题是,当我 select 除了第一个组件中的第一个选择之外的任何其他内容时,第二个组件的数据为空。当第一个组件中的第一个选择是 selected 时,我在第二个组件中将所有数组一个接一个地放在一起,但我只希望每个组件数据有一个数组。
这是我在 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]];
}
}
当我的第一个组件 selection 是首选 "Type 1" 时,在我的第二个组件中,我看到了来自 NSMutableArray "maListeSousTypes" 和 "maListeSousTypes2" 的数据,就像这里一样: Screenshot
当 "Type 2" 从第一个组件 select 编辑时,我的第二个组件是空的。
这必须比您正在做的更简单。让我通过一个简单的例子向您展示我是如何做到的:
@interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *leftComponentItems; // Items from this array will populate the left component
@property (nonatomic, strong) NSArray *rightComponentItemsA; // Items from this array will populate the right component, when the 1st row of the left component is selected
@property (nonatomic, strong) NSArray *rightComponentItemsB; // Items from this array will populate the right component, when the 2nd row of the left component is selected
@property (nonatomic, assign) NSInteger selectedLeftRow; // This keeps track of the selected row on the left component (you could take that from the datasource as well)
@end
然后在您的实施中:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.selectedLeftRow = 0;
self.leftComponentItems = @[@"first", @"second"];
self.rightComponentItemsA = @[@"A1", @"A2", @"A3", @"A4", @"A5"];
self.rightComponentItemsB = @[@"B1", @"B2", @"B3", @"B4", @"B5", @"B6", @"B7"];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2; // We have left & right components, this will always be "2"
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return self.leftComponentItems.count;
}
if (component == 1) {
if (self.selectedLeftRow == 0) {
return self.rightComponentItemsA.count;
} else {
return self.rightComponentItemsB.count;
}
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
if (component == 0) {
return self.leftComponentItems[row];
}
if (component == 1) {
if (self.selectedLeftRow == 0) {
return self.rightComponentItemsA[row];
} else {
return self.rightComponentItemsB[row];
}
}
return @"default";
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
self.selectedLeftRow = row; // We keep track of the selected row
[pickerView reloadComponent:1]; // When we select a row in the left component, update the data on the right component
}
}
@end
有什么不明白的地方尽管问。