@interface HomeViewController : UIViewController
@property (copy, nonatomic) NSString *state;
@property (copy, nonatomic) NSString *city;
@property (copy, nonatomic) NSString *district;
@end
@interface HomeViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
NSArray *provinces, *cities, *areas;
}
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//宽和高是固定的,宽:320,高216
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-400, 0, 0)];
pickerView.delegate = self;
pickerView.dataSource = self;
pickerView.showsSelectionIndicator = YES;
[self.view addSubview:pickerView];
//获取plist文件路径,两种方式
NSBundle *bundle = [NSBundle mainBundle];
// NSString *path = [[bundle resourcePath] stringByAppendingPathComponent:@"area.plist"];
NSString *path = [bundle pathForResource:@"area" ofType:@"plist"];
//读取文件内容
provinces = [[NSArray alloc]initWithContentsOfFile:path];
cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
//设置初始值
self.state = [[provinces objectAtIndex:0] objectForKey:@"state"]; //省
self.city = [[cities objectAtIndex:0] objectForKey:@"city"]; //市
//地区
if (areas.count > 0) {
self.district = [areas objectAtIndex:0];
} else{
self.district = @"";
}
}
//列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
//行数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
switch (component) {
case 0:
return [provinces count];
break;
case 1:
return [cities count];
break;
default:
return [areas count];
break;
}
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
switch (component) {
case 0:
return [[provinces objectAtIndex:row] objectForKey:@"state"];
break;
case 1:
return [[cities objectAtIndex:row] objectForKey:@"city"];
break;
case 2:
if ([areas count] > 0) {
return [areas objectAtIndex:row];
break;
}
default:
return @"";
break;
}
}
//选择行事件,监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component) {
case 0:
cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];
//将指定的行滚动到中心
[pickerView selectRow:0 inComponent:1 animated:YES];
//刷新指定列中的行
[pickerView reloadComponent:1];
areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:YES];
[pickerView reloadComponent:2];
self.state = [[provinces objectAtIndex:row] objectForKey:@"state"];
self.city = [[cities objectAtIndex:0] objectForKey:@"city"];
if ([areas count] > 0) {
self.district = [areas objectAtIndex:0];
} else{
self.district = @"";
}
break;
case 1:
areas = [[cities objectAtIndex:row] objectForKey:@"areas"];
[pickerView selectRow:0 inComponent:2 animated:YES];
[pickerView reloadComponent:2];
self.city = [[cities objectAtIndex:row] objectForKey:@"city"];
if ([areas count] > 0) {
self.district = [areas objectAtIndex:0];
} else{
self.district = @"";
}
break;
case 2:
if ([areas count] > 0) {
self.district = [areas objectAtIndex:row];
} else{
self.district = @"";
}
break;
default:
break;
}
NSInteger selectedProvinceIndex = [pickerView selectedRowInComponent:0];
NSString *selectedState = [[provinces objectAtIndex:selectedProvinceIndex]objectForKey:@"state"];
NSInteger selectedCityIndex = [pickerView selectedRowInComponent:1];
NSString *selectedCity = [[cities objectAtIndex:selectedCityIndex]objectForKey:@"city"];
NSInteger selectedAreaIndex = [pickerView selectedRowInComponent:2];
NSString *selectedArea = [areas objectAtIndex:selectedAreaIndex];
NSLog(@"State =%@ ,City = %@,Area = %@",selectedState,selectedCity,selectedArea);
}
//设置列宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
if (component == 0) {
return 60;
}
return 120;
}
//设置行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 60;
}
//自定义行中显示的View
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view{
// if (component == 0) {
// UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 60, 44)];
// view.backgroundColor = [UIColor cyanColor];
// return view;
// }
// return nil;
//}