天天看點

swift3-省、市、區位址選擇器,plist格式資料(UIPickerView)

【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
    swift3-省、市、區位址選擇器,plist格式資料(UIPickerView)

【Preparation】

Before we start ,we need a plist which contains all address information.

The data format is as follows:

swift3-省、市、區位址選擇器,plist格式資料(UIPickerView)

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[] as! String
        self.citiesList = self.provincesList[selectedProvince] as! NSDictionary
        self.citiesData = self.citiesList.allKeys as NSArray!
        self.selectedCity = self.citiesData[] as! String
        self.districtsData = self.citiesList[selectedCity] as! NSArray
        self.selectedDistrict = self.districtsData[] 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 
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component  ==  {
            return self.provincesData.count
        }else if component ==  {
            return self.citiesData.count
        }else{
            return self.districtsData.count
        }
    }

    //UIPickerViewDelegate
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if component  ==  {
            return self.provincesData[row] as? String
        }else if component ==  {
            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  ==  {
            self.selectedProvince = self.provincesData[row] as! String
            self.citiesList = self.provincesList[selectedProvince] as! NSDictionary
            self.citiesData = citiesList.allKeys as NSArray
            self.selectedCity = self.citiesData[] as! String
            self.districtsData = self.citiesList[selectedCity] as! NSArray
            self.selectedDistrict = self.districtsData[] as! String
            self.addressPickView.reloadComponent()
            self.addressPickView.reloadComponent()
            NSLog("選擇的省"+selectedProvince)
        }else if component ==  {
            self.selectedCity = self.citiesData[row] as! String
            self.districtsData = self.citiesList[selectedCity] as! NSArray
            self.selectedDistrict = self.districtsData[] as! String
            self.addressPickView.reloadComponent()

            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 blog http://blog.csdn.net/nc_jingjing/article/details/62444922
  • My technical exchange qq group:457156158
  • My weibo http://weibo.com/2397939444/profile?topnav=1&wvr=6&is_all=1
  • My Wechat public number :ITNotesShare
swift3-省、市、區位址選擇器,plist格式資料(UIPickerView)