UIPickerView的多级联动问题有些地方需要注意,其实多级联动并不难。。。楼主因为项目需要又没法使用网上的第三方,所以最近写了一个省市区多级联动,还是手写代码好!!
为了演示效果,我会多定义几个属性,表现演示效果
@interface newTeamSettingController()<UIPickerViewDataSource,UIPickerViewDelegate> @property(strong,nonatomic)NSString* strWeek;//记录周数 @property(strong,nonatomic)NSString* strTime;//记录时辰 @property(strong,nonatomic)NSArray* timeArray;//数组保存 @property(strong,nonatomic)UITextField* timeField; @end
初始化,使textField一开始显示数据
- (void)viewDidLoad { //初始化timeArray NSArray* weekArray=@[@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",@"周日"]; NSArray* shijianArray=@[@"上午",@"下午",@"晚上"]; self.timeArray=[NSArray arrayWithObjects:weekArray,shijianArray, nil]; //请选择的时间textField self.timeField=[[UITextField alloc]initWithFrame:CGRectMake(470*luScaleW, 30*luScaleH, 192*luScaleW, 30*luScaleH)]; self.timeField.tag=1; self.timeField.placeholder=@"请选择"; self.timeField.textAlignment=NSTextAlignmentRight; self.timeField.adjustsFontSizeToFitWidth=YES; self.strWeek=[NSString stringWithFormat:@"%@",self.timeArray[0][0]]; self.strTime=[NSString stringWithFormat:@"%@",self.timeArray[1][0]]; [self.chuZhenView addSubview:self.timeField]; //timePickerView self.timePickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.timeField.frame), LuW, 60)]; self.timePickerView.tag=1; self.timePickerView.dataSource=self; self.timePickerView.delegate=self; self.timeField.delegate=self; self.timeField.inputView=self.timePickerView; //在这里设置下方数据刷新部分的初始显示 for (int component = 0; component<self.timeArray.count; component++) { [self pickerView:nil didSelectRow:0 inComponent:component]; } [self pickerView:nil didSelectRow:0 inComponent:0]; self.doneBtn=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, LuW, 44)]; UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil]; UIBarButtonItem* rightBtn=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneClick)]; self.doneBtn.items=[NSArray arrayWithObjects:button1,rightBtn, nil]; self.timeField.inputAccessoryView=self.doneBtn; }
UIPickerView的代理方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return self.timeArray.count; } //每列对应多少行 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { NSArray* arryM=self.timeArray[component]; return arryM.count; } //每行对应显示的数据是什么 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { NSArray* arrayM=self.timeArray[component]; NSString* name=arrayM[row]; return name; } -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (component==0) { [pickerView reloadComponent:1]; [pickerView selectRow:0 inComponent:1 animated:YES]; _strWeek=self.timeArray[component][row]; _strTime=self.timeArray[1][0]; } else { _strTime=self.timeArray[component][row]; } self.timeField.text=[NSString stringWithFormat:@"%@ %@",_strWeek,_strTime]; }
完成。