我的视图控制器中有 3 个选取器视图选取器1包含2行音频和视频.picker2包含产品类型,如果在选取器1中选择音频,则选取器2应填充(transistor,walkman,ipod
),如果在拾取器1中选择视频,则选取器2应填充(tv,screen,monitor
),如果在拾取器2中选择了随身听,则拾取器3应填充(sony,philips,jbl
)。我对 if 条件感到困惑....
if ([[categoryArray objectAtIndex:row] isEqual:@"Audio"] && [[audioProduct objectAtIndex:row] isEqual:@"walkman"])
{
NSLog(@"Audio and walkman");
modelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walman3", nil];
[modelPickerView reloadAllComponents];
}
else if ([[categoryArray objectAtIndex:row] isEqual:@"Audio"] && [[audioProduct objectAtIndex:row] isEqual:@"mp3"]) {
modelArray=[[NSMutableArray alloc]initWithObjects:@"mp3",@"mp3", @"mp3",@"mp3",nil];
[modelPickerView reloadAllComponents];
}
您必须有条件地加载选取器 2 和选取器 3,如下所示:
picker1Array=[[NSMutableArray alloc] initWithObjects:@"Audio",@"Video", nil]
if ([[picker1array objectAtIndex:[picker1 selectedRowInComponent:0]] isEqual:@"Audio"])
{
picker2Array=[[NSMutableArray alloc] initWithObjects:@"transistor",@"walkman",@"ipod", nil];
}
else {
picker2Array=[[NSMutableArray alloc] initWithObjects:@"tv",@"screen",@"monitor", nil];
}
[picker2 reloadAllComponents];
if ([[picker2array objectAtIndex:[picker2 selectedRowInComponent:0]] isEqual:@"walkman"]){
picker3Array=[[NSMutableArray alloc] initWithObjects:@"sony",@"philips",@"jbl", nil];
}
[picker3 reloadAllComponents];
请尝试以下代码。
在 .h 文件中,请声明
NSMutableArray *randCatSel;
NSMutableArray *randSubCatSel;
NSMutableArray *randItemSel;
UIPickerView *packerView;
并在 .m 文件中编写此代码。
- (void)fetchedData{
randCatSel =[[NSMutableArray alloc] initWithObjects:@"Audio",@"Video", nil];
packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
packerView.delegate=self;
packerView.dataSource=self;
packerView.tag=1;
[self.view addSubview:packerView];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component
{
if(pickerView.tag==1)
{
return [randCatSel count];
}
else if(pickerView.tag==2)
{
return [randSubCatSel count];
}
else if(pickerView.tag==3)
{
return [randItemSel count];
}
else
{
return 0;
}
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
return [randCatSel objectAtIndex:row];
}
else if(pickerView.tag==2)
{
return [randSubCatSel objectAtIndex:row];
}
else if(pickerView.tag==3)
{
return [randItemSel objectAtIndex:row];
}
else
{
return 0;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView.tag==1)
{
if(randSubCatSel.count>0)
{
randSubCatSel=nil;
}
if([[randCatSel objectAtIndex:row] isEqualToString:@"Audio"])
{
randSubCatSel=[[NSMutableArray alloc] initWithObjects:@"Transistor",@"walkman",@"ipod", nil];
}
else
{
randSubCatSel=[[NSMutableArray alloc] initWithObjects:@"TV",@"Screen",@"Monitor", nil];
}
[packerView removeFromSuperview];
packerView=[[UIPickerView alloc] initWithFrame:CGRectMake(0, 280, 320, 200)];
packerView.delegate=self;
packerView.dataSource=self;
packerView.tag=2;
[self.view addSubview:packerView];
}
else if(pickerView.tag==2)
{
//do coding according to randSubCatSel for randItemSel
}
}
请注意,这是满足您要求的示例代码。 您需要相应地进行更改。我希望这段代码对您有所帮助。