我需要创建三个独立的pickerView。第一个pickerView是categoryPickerView。选择值时,应加载其他2个pickerView(productPickerview和modelPickerview)未正确加载数据。
didSelectRow回调被触发但我的NSLog已经证明它没有通过第一个if语句检查它是否等于“Audio”
对于catogeryPickerView,我在viewDidLoad中为它编写了以下数组: -
devicecatogery=[[NSArray alloc]initWithObjects:@"Audio",@"Video", nil];
//code for numeric keypad done button
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 44, 320, 300)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
nil];
[numberToolbar sizeToFit];
// catogery pickerview
categoryPickerView=[[UIPickerView alloc]init];
categoryPickerView.delegate=self;
categoryPickerView.tag=1;
txtCategory.inputView=categoryPickerView;
txtCategory.inputAccessoryView=numberToolbar;
//product pickerview
productPickerView=[[UIPickerView alloc]init];
productPickerView.delegate=self;
productPickerView.tag=2;
txtProduct.inputView=productPickerView;
txtProduct.inputAccessoryView=numberToolbar;
//model pickerview
modelPickerView=[[UIPickerView alloc]init];
modelPickerView.delegate=self;
modelPickerView.tag=3;
txtModel.inputView=modelPickerView;
txtModel.inputAccessoryView=numberToolbar;
}//end of view did load
这是我的选择器视图事件: -
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (pickerView.tag == 1) {
return [devicecatogery count]; //<-----------this is for category picker view
}else if(pickerView.tag == 2)
return [commanProductArray count]; //<-----------this is for product picker view
else
return [commanModelArray count]; //<-------------- this is for model picker view
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (pickerView.tag == 1)
return [devicecatogery objectAtIndex:row];//<----this is for category picker view
else if (pickerView.tag == 2)
return [commanProductArray objectAtIndex:row];//<--------this is for product picker view
else
return [commanModelArray objectAtIndex:row];//<--------- this is for model picker view
}
这是我的didSelect回调:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if ([[categoryArray objectAtIndex:[categoryPickerView selectedRowInComponent:0] ] isEqual:@"Audio"]) {
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]] isEqual:@"walkman"]) {
// walkman list display
[productPickerView reloadAllComponents];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
[modelPickerView reloadAllComponents];
}else if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]] isEqual:@"mp3"]){
//mp3 list display
[productPickerView reloadAllComponents];
[modelPickerView reloadAllComponents];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"mp3",@"mp3",@"mp3",@"mp3", nil];
}else if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]] isEqual:@"ipod"]){
//ipod list display
[productPickerView reloadAllComponents];
[modelPickerView reloadAllComponents];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"ipod1",@"ipod2",@"ipod3",@"ipod4", nil];
}
}else if ([[categoryArray objectAtIndex:[categoryPickerView selectedRowInComponent:0]] isEqual:@"Video"]){
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"TV",@"Moniter",@"Projector", nil];
if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]]isEqual:@"TV"]) {
//for tv model display
[productPickerView reloadAllComponents];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"TV1",@"TV2",@"TV3", nil];
[modelPickerView reloadAllComponents];
}else if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]]isEqual:@"Moniter"]){
//for moniter model display
[productPickerView reloadAllComponents];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Moniter1",@"Moniter2",@"Moniter3", nil];
[modelPickerView reloadAllComponents];
}else if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]] isEqual:@"Projector"]){
//for projector display
[productPickerView reloadAllComponents];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Projector1",@"Projector2",@"Projector3", nil];
[modelPickerView reloadAllComponents];
}
}
}
答案 0 :(得分:1)
这段代码对我没有意义,我怀疑你的问题在这里:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if ([[categoryArray objectAtIndex:[categoryPickerView selectedRowInComponent:0] ] isEqual:@"Audio"]) {
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
if ([[commanProductArray objectAtIndex:[productPickerView selectedRowInComponent:0]] isEqual:@"walkman"]) {
// walkman list display
[productPickerView reloadAllComponents];
选中时,所有pickerView都会进入此回调。你首先没有检查标签,看看它是哪个pickerView,然后你检查选择的行是否是音频/视频,如果它是第二个/第三个选择器,代码永远不会通过它,因为它们不会有选择音频/视频。
遵循逻辑,如果选择“音频”,则将commanProductArray
设置为给定值,然后在加载之前立即检查所选值是否等于“walkman”。
[productPickerView reloadAllComponents];
后我应该直接调用 commanProductArray
,因为你设置数组并在加载之前检查选择了哪个值。
这个问题可以通过简单地添加2个或3个NSlog
来查看它是否已进入循环
修改强>
您的代码应遵循以下内容:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// If first picker
if(pickerView.tag == 1)
{
// If Audio
if(row == 0)
{
// Load result for audio
}
// If Video
else if (row == 1)
{
// load result for video
}
}
// If second picker
else if(pickerView.tag == 2)
{
// if Walkman
if(row == 0)
{
// load result for walkman
}
// etc etc etc
}
else if (pickerView.tag == 3)
{
}
}
答案 1 :(得分:0)
试试这个:
- (void)viewDidLoad
{
devicecatogery=[[NSArray alloc]initWithObjects:@"Audio",@"Video", nil];
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (pickerView.tag==1) {
return devicecatogery.count;
}
else if(pickerView.tag==2){
return commanProductArray.count;
}
else if (pickerView.tag==3)
{
return commanModelArray.count;
}
return 0;
}
// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// tell the picker the title for a given component
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
if (pickerView.tag==1)
{
title = [NSString stringWithFormat:@"%@",[devicecatogery objectAtIndex:row]];
}
else if (pickerView.tag==2)
{
if([categoryPickerView selectedRowInComponent:0]==0)
{
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"walkman",@"mp3",@"ipod", nil];
}
else{
commanProductArray=[[NSMutableArray alloc]initWithObjects:@"TV",@"Moniter",@"Projector", nil];
}
title = [NSString stringWithFormat:@"%@",[commanProductArray objectAtIndex:row]];
}
else if (pickerView.tag==3)
{
if ([[devicecatogery objectAtIndex:[categoryPickerView selectedRowInComponent:0]] isEqualToString:@"Audio"]) {
if([productPickerview selectedRowInComponent:0]==0)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"walkman1",@"walkman2",@"walkman3",@"walkman4", nil];
}
else if([productPickerview selectedRowInComponent:0]==1)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"mp31",@"mp32",@"mp33",@"mp34", nil];
}
else if([productPickerview selectedRowInComponent:0]==2)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"ipod1",@"ipod2",@"ipod3",@"ipod4", nil];
}
}
else
{
if([productPickerview selectedRowInComponent:0]==0)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"TV1",@"TV2",@"TV3",@"TV4", nil];
}
else if([productPickerview selectedRowInComponent:0]==1)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Moniter1",@"Moniter2",@"Moniter3",@"Moniter4", nil];
}
else if([productPickerview selectedRowInComponent:0]==2)
{
commanModelArray=[[NSMutableArray alloc]initWithObjects:@"Projector1",@"Projector2",@"Projector3",@"Projector4", nil];
}
}
title = [NSString stringWithFormat:@"%@",[commanModelArray objectAtIndex:row]];
}
return title;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if (pickerView.tag==1)
{
[productPickerview reloadAllComponents];
[modelPickerview reloadAllComponents];
}
if (pickerView.tag==2) {
[modelPickerview reloadAllComponents];
}
// Handle the selection
}
有关详细信息,您需要查看它:Sample code
愿它能帮到你。
快乐的编码......:)