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(*luScaleW, *luScaleH, *luScaleW, *luScaleH)];
self.timeField.tag=;
self.timeField.placeholder=@"请选择";
self.timeField.textAlignment=NSTextAlignmentRight;
self.timeField.adjustsFontSizeToFitWidth=YES;
self.strWeek=[NSString stringWithFormat:@"%@",self.timeArray[][]];
self.strTime=[NSString stringWithFormat:@"%@",self.timeArray[][]];
[self.chuZhenView addSubview:self.timeField];
//timePickerView
self.timePickerView=[[UIPickerView alloc]initWithFrame:CGRectMake(, CGRectGetMaxY(self.timeField.frame), LuW, )];
self.timePickerView.tag=;
self.timePickerView.dataSource=self;
self.timePickerView.delegate=self;
self.timeField.delegate=self;
self.timeField.inputView=self.timePickerView;
//在这里设置下方数据刷新部分的初始显示
for (int component = ; component<self.timeArray.count; component++) {
[self pickerView:nil didSelectRow: inComponent:component];
}
[self pickerView:nil didSelectRow: inComponent:];
self.doneBtn=[[UIToolbar alloc]initWithFrame:CGRectMake(, , LuW, )];
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==) {
[pickerView reloadComponent:];
[pickerView selectRow: inComponent: animated:YES];
_strWeek=self.timeArray[component][row];
_strTime=self.timeArray[][];
}
else
{
_strTime=self.timeArray[component][row];
}
self.timeField.text=[NSString stringWithFormat:@"%@ %@",_strWeek,_strTime];
}

完成。

05-08 14:57