美文网首页
swift3-省、市、区地址选择器,plist格式数据(UIPi

swift3-省、市、区地址选择器,plist格式数据(UIPi

作者: 宇宙超级无敌小小静 | 来源:发表于2017-03-21 14:44 被阅读199次

    【Summary】

    This article mainly introduces the basic usage of UIPickerView, we can obtain address information by selecting the provinces ,cities and districts.

    【Explanation and Effect Picture】

    1. The cities and districts will change when you switch the province
    2. Districts will change when you switch the city
    3. Click the confirm button,then the information label will show the address you choose


    【Preparation】

    Before we start ,we need a plist which contains all address information.
    The data format is as follows:


    This is part of the source code:

    <dict>
        <key>河北省</key>
        <dict>
            <key>石家庄市</key>
            <array>
                <string>长安区</string>
                <string>桥东区</string>
                <string>桥西区</string>
                <string>新华区</string>
                <string>井陉矿区</string>
                <string>裕华区</string>
                <string>井陉县</string>
                <string>正定县</string>
                <string>栾城县</string>
                <string>行唐县</string>
                <string>灵寿县</string>
                <string>高邑县</string>
                <string>深泽县</string>
                <string>赞皇县</string>
                <string>无极县</string>
                <string>平山县</string>
                <string>元氏县</string>
                <string>赵县</string>
                <string>辛集市</string>
                <string>藁城市</string>
                <string>晋州市</string>
                <string>新乐市</string>
                <string>鹿泉市</string>
            </array>
            <key>唐山市</key>
            <array>
                <string>路南区</string>
                <string>路北区</string>
                <string>古冶区</string>
                <string>开平区</string>
                <string>丰南区</string>
                <string>丰润区</string>
                <string>滦县</string>
                <string>滦南县</string>
                <string>乐亭县</string>
                <string>迁西县</string>
                <string>玉田县</string>
                <string>唐海县</string>
                <string>遵化市</string>
                <string>迁安市</string>
            </array>
            <key>秦皇岛市</key>
            <array>
                <string>海港区</string>
                <string>山海关区</string>
                <string>北戴河区</string>
                <string>青龙满族自治县</string>
                <string>昌黎县</string>
                <string>抚宁县</string>
                <string>卢龙县</string>
            </array>
            <key>邯郸市</key>
            <array>
                <string>邯山区</string>
                <string>丛台区</string>
                <string>复兴区</string>
                <string>峰峰矿区</string>
                <string>邯郸县</string>
                <string>临漳县</string>
                <string>成安县</string>
                <string>大名县</string>
                <string>涉县</string>
                <string>磁县</string>
                <string>肥乡县</string>
                <string>永年县</string>
                <string>邱县</string>
                <string>鸡泽县</string>
                <string>广平县</string>
                <string>馆陶县</string>
                <string>魏县</string>
                <string>曲周县</string>
                <string>武安市</string>
            </array>
            <key>邢台市</key>
            <array>
                <string>桥东区</string>
                <string>桥西区</string>
                <string>邢台县</string>
                <string>临城县</string>
                <string>内丘县</string>
                <string>柏乡县</string>
                <string>隆尧县</string>
                <string>任县</string>
                <string>南和县</string>
                <string>宁晋县</string>
                <string>巨鹿县</string>
                <string>新河县</string>
                <string>广宗县</string>
                <string>平乡县</string>
                <string>威县</string>
                <string>清河县</string>
                <string>临西县</string>
                <string>南宫市</string>
                <string>沙河市</string>
            </array>
            <key>保定市</key>
            <array>
                <string>新市区</string>
                <string>北市区</string>
                <string>南市区</string>
                <string>满城县</string>
                <string>清苑县</string>
                <string>涞水县</string>
                <string>阜平县</string>
                <string>徐水县</string>
                <string>定兴县</string>
                <string>唐县</string>
                <string>高阳县</string>
                <string>容城县</string>
                <string>涞源县</string>
                <string>望都县</string>
                <string>安新县</string>
                <string>易县</string>
                <string>曲阳县</string>
                <string>蠡县</string>
                <string>顺平县</string>
                <string>博野县</string>
                <string>雄县</string>
                <string>涿州市</string>
                <string>定州市</string>
                <string>安国市</string>
                <string>高碑店市</string>
            </array>
            <key>张家口市</key>
            <array>
                <string>桥东区</string>
                <string>桥西区</string>
                <string>宣化区</string>
                <string>下花园区</string>
                <string>宣化县</string>
                <string>张北县</string>
                <string>康保县</string>
                <string>沽源县</string>
                <string>尚义县</string>
                <string>蔚县</string>
                <string>阳原县</string>
                <string>怀安县</string>
                <string>万全县</string>
                <string>怀来县</string>
                <string>涿鹿县</string>
                <string>赤城县</string>
                <string>崇礼县</string>
            </array>
            <key>承德市</key>
            <array>
                <string>双桥区</string>
                <string>双滦区</string>
                <string>鹰手营子矿区</string>
                <string>承德县</string>
                <string>兴隆县</string>
                <string>平泉县</string>
                <string>滦平县</string>
                <string>隆化县</string>
                <string>丰宁满族自治县</string>
                <string>宽城满族自治县</string>
                <string>围场满族蒙古族自治县</string>
            </array>
            <key>沧州市</key>
            <array>
                <string>新华区</string>
                <string>运河区</string>
                <string>沧县</string>
                <string>青县</string>
                <string>东光县</string>
                <string>海兴县</string>
                <string>盐山县</string>
                <string>肃宁县</string>
                <string>南皮县</string>
                <string>吴桥县</string>
                <string>献县</string>
                <string>孟村回族自治县</string>
                <string>泊头市</string>
                <string>任丘市</string>
                <string>黄骅市</string>
                <string>河间市</string>
            </array>
            <key>廊坊市</key>
            <array>
                <string>安次区</string>
                <string>广阳区</string>
                <string>固安县</string>
                <string>永清县</string>
                <string>香河县</string>
                <string>大城县</string>
                <string>文安县</string>
                <string>大厂回族自治县</string>
                <string>霸州市</string>
                <string>三河市</string>
            </array>
            <key>衡水市</key>
            <array>
                <string>桃城区</string>
                <string>枣强县</string>
                <string>武邑县</string>
                <string>武强县</string>
                <string>饶阳县</string>
                <string>安平县</string>
                <string>故城县</string>
                <string>景县</string>
                <string>阜城县</string>
                <string>冀州市</string>
                <string>深州市</string>
            </array>
        </dict>
    
    

    【Function Realization】

    1. Get data from address.plist,and save it as NSDictionary in the local
      2.The addressPickView will load all provinces, we choose the first city and the first district of the city as default
    2. Realize the func of UIPickerViewDelegate and UIPickerViewDataSource,refresh the addressPickView by reloadComponent()
    3. Realize the click action of the confirm button
    
    import UIKit
    class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource{
        
        //pickerview 视图
        @IBOutlet weak var addressPickView: UIPickerView!
        
         //用于显示地址的label
        @IBOutlet weak var addressLabel: UILabel!
        
        //省 数组
        var provincesData:NSArray!
        //市 数组
        var citiesData:NSArray!
        //区 数组
        var districtsData:NSArray!
        
        //省 字典
        var provincesList:NSDictionary!
        //市 字典
        var citiesList:NSDictionary!
        
        //选择的省
        var selectedProvince:String!
        //选择的市
        var selectedCity:String!
        //选择的区
        var selectedDistrict:String!
        
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            
            
            //得到本地文件路径
            let addressPath = Bundle.main.path(forResource: "address", ofType: "plist")
            //得到字典类型数据
            let dicPList = NSDictionary(contentsOfFile: addressPath!)!
            
            self.provincesList = dicPList
            self.provincesData = self.provincesList.allKeys as NSArray!
            
            //初始化
            //默认取出第一个省、市、区的数据
            self.selectedProvince = self.provincesData[0] as! String
            self.citiesList = self.provincesList[selectedProvince] as! NSDictionary
            self.citiesData = self.citiesList.allKeys as NSArray!
            self.selectedCity = self.citiesData[0] as! String
            self.districtsData = self.citiesList[selectedCity] as! NSArray
            self.selectedDistrict = self.districtsData[0] as! String
            
            self.addressPickView.delegate = self
            self.addressPickView.dataSource = self
            
            
        }
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
        
       //确认按钮,显示最新地址
        @IBAction func commitTapped(_ sender: Any) {
            self.addressLabel.text = "\(self.selectedProvince!)"+"\(self.selectedCity!)"+"\(self.selectedDistrict!)"
        }
        
        //UIPickerViewDataSource
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 3
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            if component  == 0 {
                return self.provincesData.count
            }else if component == 1 {
                return self.citiesData.count
            }else{
                return self.districtsData.count
            }
        }
        
        //UIPickerViewDelegate
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            if component  == 0 {
                return self.provincesData[row] as? String
            }else if component == 1 {
                return self.citiesData[row] as? String
            }else{
                return self.districtsData[row] as? String
            }
        }
        
        
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if component  == 0 {
                self.selectedProvince = self.provincesData[row] as! String
                self.citiesList = self.provincesList[selectedProvince] as! NSDictionary
                self.citiesData = citiesList.allKeys as NSArray
                self.selectedCity = self.citiesData[0] as! String
                self.districtsData = self.citiesList[selectedCity] as! NSArray
                self.selectedDistrict = self.districtsData[0] as! String
                self.addressPickView.reloadComponent(1)
                self.addressPickView.reloadComponent(2)
                NSLog("选择的省"+selectedProvince)
            }else if component == 1 {
                self.selectedCity = self.citiesData[row] as! String
                self.districtsData = self.citiesList[selectedCity] as! NSArray
                self.selectedDistrict = self.districtsData[0] as! String
                self.addressPickView.reloadComponent(2)
                
                NSLog("选择的市"+selectedCity)
            }else{
                self.selectedDistrict = self.districtsData[row] as! String
                NSLog("选择的区"+selectedDistrict)
            }
        }
        
    }
    
    
    

    【Download Source Code】

    http://download.csdn.net/detail/nc_jingjing/9788521

    【About me】

    • My technical exchange qq group:457156158

    相关文章

      网友评论

          本文标题:swift3-省、市、区地址选择器,plist格式数据(UIPi

          本文链接:https://www.haomeiwen.com/subject/waownttx.html