我有一个3组件选择器视图,效果很好。但是,当用户从第一个组件中选择选项1时,我想要一个功能,第二个组件显示某些数据。但是,如果用户从第一个组件中选择选项2,则第二个组件显示不同的数据。
基本上我的代码ID就像这样:
在组件1中选择“green”显示组件2中的others.easonArray数据
在component1中选择“black”表示component2
中的choiceArray数据如果有意义的话?
这就是我到目前为止.h:
#import <UIKit/UIKit.h>
@interface view : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *picker2;
@end
和.m
#import "view2.h"
@interface view ()
{
NSArray*othercolourArray;
NSArray*otherseasonArray;
NSArray*otherotherArray;
NSArray*choiceArray;
}
@end
@implementation view
- (void)viewDidLoad {
[super viewDidLoad];
othercolourArray =[[NSArray alloc]initWithObjects:@"green", @"black", nil];
otherseasonArray =[[NSArray alloc]initWithObjects:@"summer", @"winter", nil];
otherotherArray =[[NSArray alloc]initWithObjects:@"triangle", @"circle", nil];
choiceArray =[[NSArray alloc]initWithObjects:@"choice1", @"choice2", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
switch (component) {
case 0:
return othercolourArray.count;
break;
case 1:
return otherseasonArray.count;
break;
case 2:
return otherotherArray.count;
break;
default:
break;
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
case 0:
return [othercolourArray objectAtIndex:row];
break;
case 1:
return [otherseasonArray objectAtIndex:row];
break;
case 2:
return [otherotherArray objectAtIndex:row];
break;
default:
break;
}
return 0;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)
我已根据您的要求编辑了您的代码。你可以使用这个.. 如果我有你的观点,你会看到这种行为: 选择&#34;绿色&#34;在组件1中,将在组件2中显示others.easonArray数据
choosing "black" in component1, Will show you choiceArray data in component2
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
{
NSArray*othercolourArray;
NSArray*otherseasonArray;
NSArray*otherotherArray;
NSArray*choiceArray;
NSInteger selectedOptionFromColumn1;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
othercolourArray =[[NSArray alloc]initWithObjects:@"green", @"black",@"testColumn11" ,nil];
otherseasonArray =[[NSArray alloc]initWithObjects:@"summer", @"winter",@"testColumn22", nil];
otherotherArray =[[NSArray alloc]initWithObjects:@"triangle", @"circle",@"testColumn33", nil];
choiceArray =[[NSArray alloc]initWithObjects:@"choice11", @"choice22",@"choice34", nil];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) {
selectedOptionFromColumn1=[pickerView selectedRowInComponent:0];
[pickerView reloadComponent:1];
}
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
switch (component) {
case 0:
return othercolourArray.count;
break;
case 1:
if (selectedOptionFromColumn1 == 0) {
return otherseasonArray.count;
}
else if (selectedOptionFromColumn1 == 1){
return choiceArray.count;
}
break;
case 2:
return otherotherArray.count;
break;
default:
break;
}
return 0;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
switch (component) {
case 0:
return [othercolourArray objectAtIndex:row];
break;
case 1:
if (selectedOptionFromColumn1 == 0) {
return [otherseasonArray objectAtIndex:row];
}
else if (selectedOptionFromColumn1 == 1)
{
return [choiceArray objectAtIndex:row];
}
break;
case 2:
return [otherotherArray objectAtIndex:row];
break;
default:
break;
}
return 0;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end