318 lines
9.3 KiB
Objective-C
318 lines
9.3 KiB
Objective-C
//
|
|
// LocationPickerView.m
|
|
// CustomPicker
|
|
//
|
|
// Created by ko1o on 2018/7/24.
|
|
// Copyright © 2018年 ko1o. All rights reserved.
|
|
//
|
|
|
|
#import "FSLocationPickerView.h"
|
|
#import "FSLocationDataManager.h"
|
|
|
|
static CGFloat PickerViewHeight = 216;
|
|
|
|
@interface FSLocationPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
|
|
|
|
@property(nonatomic,strong) FSLocationDataManager *dataManager;
|
|
|
|
@property(nonatomic,strong) UIPickerView *pickerView;
|
|
|
|
@property(nonatomic,strong) NSMutableArray *firstComponentArray;
|
|
@property(nonatomic,strong) NSMutableArray<NSString *> *provinceArray;
|
|
@property(nonatomic,strong) NSMutableArray<NSString *> *cityArray;
|
|
@property(nonatomic,strong) NSMutableArray<NSString *> *areaArray;
|
|
|
|
@property(nonatomic,assign) NSInteger currentComponents;
|
|
@property(nonatomic,assign) NSInteger currentFirstIndex;
|
|
@property(nonatomic,assign) NSInteger currentSecondIndex;
|
|
@property(nonatomic,assign) NSInteger currentThirdIndex;
|
|
|
|
@end
|
|
|
|
@implementation FSLocationPickerView
|
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
{
|
|
self = [super initWithFrame:frame];
|
|
if(self)
|
|
{
|
|
self.dataManager = [[FSLocationDataManager alloc] init];
|
|
self.provinceArray = [@[] mutableCopy];
|
|
self.cityArray = [@[] mutableCopy];
|
|
self.areaArray = [@[] mutableCopy];
|
|
self.currentFirstIndex = self.currentSecondIndex = 0;
|
|
self.currentThirdIndex = -1;
|
|
[self fetchData];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)fetchData
|
|
{
|
|
for (NSDictionary *dictionary in self.dataManager.provincesArray) {
|
|
[self.provinceArray addObject:dictionary[@"name"]];
|
|
}
|
|
NSDictionary *cityDic = self.dataManager.provincesArray[0];
|
|
NSArray *citiesArray = cityDic[@"city"];
|
|
for (NSDictionary *dic in citiesArray) {
|
|
[self.cityArray addObject:dic[@"name"]];
|
|
}
|
|
}
|
|
#pragma mark - override
|
|
|
|
- (void)layoutSubviews
|
|
{
|
|
[super layoutSubviews];
|
|
//选择框颜色设置
|
|
for(UIView *singleLine in _pickerView.subviews)
|
|
{
|
|
if (singleLine.frame.size.height < 1)
|
|
{
|
|
singleLine.backgroundColor = UIColor.lightGrayColor;
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - public
|
|
|
|
- (void)showPickerViewWithProvince:(NSString *)province city:(NSString *)city area:(NSString *)area
|
|
{
|
|
NSInteger provinceIndex = -1; //省
|
|
NSInteger cityIndex = -1; //市
|
|
NSInteger areaIndex = -1; //区
|
|
|
|
self.cityArray = [@[] mutableCopy];
|
|
|
|
for (NSString *provinceName in self.provinceArray) {
|
|
provinceIndex += 1;
|
|
if([province hasPrefix:provinceName])
|
|
{
|
|
self.currentFirstIndex = provinceIndex;
|
|
}
|
|
}
|
|
NSArray *cityArray = ((NSDictionary *)self.dataManager.provincesArray[self.currentFirstIndex])[@"city"];
|
|
for (NSDictionary *cityDic in cityArray)
|
|
{
|
|
cityIndex += 1;
|
|
[self.cityArray addObject:cityDic[@"name"]];
|
|
if([city hasPrefix:cityDic[@"name"]])
|
|
{
|
|
self.currentSecondIndex = cityIndex;
|
|
}
|
|
}
|
|
NSArray *areaArray = ((NSDictionary *)((NSArray *)cityArray[self.currentSecondIndex]))[@"area"];
|
|
if(areaArray.count > 0)
|
|
{
|
|
self.areaArray = [areaArray mutableCopy];
|
|
for (NSString *areaInfo in areaArray)
|
|
{
|
|
areaIndex += 1;
|
|
if([area hasPrefix:areaInfo])
|
|
{
|
|
self.currentThirdIndex = areaIndex;
|
|
}
|
|
}
|
|
}
|
|
[self.pickerView reloadAllComponents];
|
|
[self.pickerView selectRow:self.currentFirstIndex inComponent:0 animated:YES];
|
|
[self.pickerView selectRow:self.currentSecondIndex inComponent:1 animated:YES];
|
|
if(self.areaArray.count > 0)
|
|
{
|
|
[self.pickerView selectRow:self.currentThirdIndex inComponent:2 animated:YES];
|
|
}
|
|
|
|
}
|
|
|
|
#pragma mark - delegate
|
|
|
|
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
|
|
{
|
|
NSInteger countOfComponents = 0;
|
|
if(self.provinceArray.count >= 1)
|
|
{
|
|
countOfComponents += 1;
|
|
}
|
|
if(self.cityArray.count >= 1)
|
|
{
|
|
countOfComponents += 1;
|
|
}
|
|
if(self.areaArray.count >= 1)
|
|
{
|
|
countOfComponents += 1;
|
|
}
|
|
else
|
|
{
|
|
self.currentThirdIndex = -1;
|
|
}
|
|
self.currentComponents = countOfComponents;
|
|
return 3;
|
|
}
|
|
|
|
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
|
|
{
|
|
if(component == 0)
|
|
{
|
|
return self.provinceArray.count;
|
|
}
|
|
else if(component == 1)
|
|
{
|
|
return self.cityArray.count >= 1 ? self.cityArray.count : self.areaArray.count;
|
|
}
|
|
else
|
|
{
|
|
return self.areaArray.count;
|
|
}
|
|
}
|
|
|
|
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
|
|
{
|
|
if(component == 0)
|
|
{
|
|
return self.provinceArray[row];
|
|
}
|
|
else if(component == 1)
|
|
{
|
|
return self.cityArray.count >= 1 ? self.cityArray[row] : self.areaArray[row];
|
|
}
|
|
else
|
|
{
|
|
return self.areaArray[row];
|
|
}
|
|
}
|
|
|
|
- (NSArray *)locationInfo
|
|
{
|
|
NSMutableArray *datasM = [NSMutableArray array];
|
|
for (int i = 0; i < _pickerView.numberOfComponents; i++) {
|
|
NSInteger row = [_pickerView selectedRowInComponent:i];
|
|
if ([_pickerView numberOfRowsInComponent:i] > row) {
|
|
[datasM addObject:[self pickerView:_pickerView titleForRow:row forComponent:i]];
|
|
}
|
|
}
|
|
return [datasM copy];
|
|
}
|
|
|
|
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
|
|
{
|
|
NSArray *citiesArray;
|
|
citiesArray = ((NSDictionary *)self.dataManager.provincesArray[self.currentFirstIndex])[@"city"];
|
|
self.firstComponentArray = [citiesArray mutableCopy];
|
|
if(component == 0)
|
|
{
|
|
citiesArray = ((NSDictionary *)self.dataManager.provincesArray[row])[@"city"];
|
|
self.firstComponentArray = [citiesArray mutableCopy];
|
|
self.currentFirstIndex = row;
|
|
}
|
|
if(component == 1)
|
|
{
|
|
citiesArray = self.firstComponentArray[row];
|
|
}
|
|
if(component == 0)
|
|
{
|
|
self.cityArray = [@[] mutableCopy];
|
|
self.areaArray = [@[] mutableCopy];
|
|
for (NSDictionary *dic in citiesArray) {
|
|
[self.cityArray addObject:dic[@"name"]];
|
|
}
|
|
if(self.cityArray.count > 0)
|
|
{
|
|
[pickerView selectRow:0 inComponent:1 animated:NO];
|
|
}
|
|
self.areaArray = [((NSDictionary *)citiesArray[0])[@"area"] mutableCopy];
|
|
if(self.areaArray.count > 0)
|
|
{
|
|
self.currentThirdIndex = 0;
|
|
if(self.currentComponents == 3)
|
|
{
|
|
[pickerView selectRow:0 inComponent:2 animated:NO];
|
|
}
|
|
}
|
|
self.currentSecondIndex = 0;
|
|
}
|
|
else if(component == 1)
|
|
{
|
|
self.areaArray = [@[] mutableCopy];
|
|
self.areaArray = [((NSDictionary *)citiesArray)[@"area"] mutableCopy];
|
|
if(self.areaArray.count > 0)
|
|
{
|
|
if(self.currentComponents == 3)
|
|
{
|
|
[pickerView selectRow:0 inComponent:2 animated:NO];
|
|
self.currentThirdIndex = 0;
|
|
}
|
|
}
|
|
self.currentSecondIndex = row;
|
|
}
|
|
else
|
|
{
|
|
self.currentThirdIndex = row;
|
|
}
|
|
[pickerView reloadAllComponents];
|
|
NSMutableArray *infoArray = [@[self.provinceArray[self.currentFirstIndex],self.cityArray[self.currentSecondIndex]] mutableCopy];
|
|
if(self.currentThirdIndex != -1)
|
|
{
|
|
[infoArray addObject:self.areaArray[self.currentThirdIndex]];
|
|
}
|
|
if([self.delegate respondsToSelector:@selector(fsLocationPickerView:locationInfo:)])
|
|
{
|
|
[self.delegate fsLocationPickerView:self locationInfo:infoArray];
|
|
}
|
|
|
|
if (self.locationPickerVauleDidChangeBlock) {
|
|
self.locationPickerVauleDidChangeBlock(self, infoArray);
|
|
}
|
|
}
|
|
|
|
#pragma mark - lazy
|
|
|
|
-(UIPickerView *)pickerView
|
|
{
|
|
if(!_pickerView)
|
|
{
|
|
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PickerViewHeight)];
|
|
_pickerView.delegate = self;
|
|
_pickerView.dataSource = self;
|
|
}
|
|
return _pickerView;
|
|
}
|
|
|
|
- (void)selectDone:(id)sender
|
|
{
|
|
NSLog(@"----");
|
|
|
|
}
|
|
- (void)cancel:(id)sender
|
|
{
|
|
|
|
}
|
|
|
|
-(UIView *)getRgAccessoryView
|
|
{
|
|
CGRect rectBar = {0,0,SCREEN_WIDTH,44};
|
|
UIView *bg = [[UIView alloc] initWithFrame:rectBar];
|
|
bg.backgroundColor = [UIColor whiteColor];
|
|
|
|
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
|
|
line.backgroundColor = RGBA(0, 0, 0, 0.1);
|
|
[bg addSubview:line];
|
|
|
|
UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
cancel.frame = CGRectMake(0, line.bottom, 60, 44);
|
|
[cancel setTitle:@"取消" forState:UIControlStateNormal];
|
|
cancel.titleLabel.font = [UIFont systemFontOfSize:17];
|
|
[cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
|
[cancel addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchUpInside];
|
|
[bg addSubview:cancel];
|
|
|
|
UIButton *done = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
done.frame = CGRectMake(rectBar.size.width - 60, line.bottom, 60, 44);
|
|
[done setTitle:@"完成" forState:UIControlStateNormal];
|
|
done.titleLabel.font = [UIFont systemFontOfSize:17];
|
|
[done setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
|
[done addTarget:self action:@selector(selectDone:) forControlEvents:UIControlEventTouchUpInside];
|
|
[bg addSubview:done];
|
|
|
|
return bg;
|
|
}
|
|
@end
|