Skip to content

iOS中国行政区域picker控件,3级联动,数据来源于高德web API

License

Notifications You must be signed in to change notification settings

enix223/EYCNCityPicker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EYCNCityPicker

iOS城市选择器,行政区域来源于高德web API。

screenshot

安装依赖

Cocoapods

Podfile 文件中增加如下行

pod 'EYCNCityPicker'

运行:

pod install

使用方法

  1. 获取行政区域数据
EYCNCityService *service = [[EYCNCityService alloc] initWithAPIKey:@"699161121d34385622a16ca815c6a934"];
    [service requestCNCitiesWithCompletion:^(NSArray * _Nullable cities, NSError * _Nullable error) {
        if (error) {
            NSLog(@"获取行政区域失败,原因: %@", error.localizedDescription);
        } else {
            self.data = cities;
        }
    }];
  1. 显示picker
    /// picker中元素的样式
    NSDictionary *attr = @{NSForegroundColorAttributeName: [UIColor darkGrayColor],
                           NSFontAttributeName: [UIFont systemFontOfSize:16]};
    
    /// 初始选择
    NSArray *selections = @[@(_selectProvinceIdx), @(_selectCityIdx), @(_selectDistrictIdx)];
    
    /// 构建一个picker
    EYCNCityPicker *picker = [EYCNCityPicker buildPicker];
    
    /// 显示picker
    [picker
     showInView:self.view
     withData:self.data
     initialSelctions:selections
     itemAttributes:attr
     cancel:^(EYCNCityPicker *picker) {
         // 用户取消选择
         NSLog(@"User canceled");
         [self.selectButton setTitle:@"未选择" forState:UIControlStateNormal];
     } confirmBlock:^(EYCNCityPicker * _Nonnull picker, NSInteger selectedProvinceIdx, NSInteger selectedCityIdx, NSInteger selectedDistrictIdx, NSString * _Nullable selectProvince, NSString * _Nullable selectCity, NSString * _Nullable selectDistrict) {
         // 用户选择了某个行政区域
         self.selectProvinceValue = selectProvince;
         self.selectCityValue = selectCity == nil ? @"" : selectCity;
         self.selectDistrictValue = selectDistrict == nil ? @"" : selectDistrict;
         
         self.selectProvinceIdx = selectedProvinceIdx;
         self.selectCityIdx = selectedCityIdx;
         self.selectDistrictIdx = selectedDistrictIdx;
         
         NSString *selection = [NSString stringWithFormat:@"%@, %@, %@", _selectProvinceValue, _selectCityValue, _selectDistrictValue];
         [self.selectButton setTitle:selection forState:UIControlStateNormal];
     }];

更多使用例子,请查看EYCNCityPickerExample

About

iOS中国行政区域picker控件,3级联动,数据来源于高德web API

Topics

Resources

License

Stars

Watchers

Forks

Packages