swift3-省、市、区地址选择器,plist格式数据(UIPickerView)
2017-03-21 16:04
253 查看
【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】
The cities and districts will change when you switch the provinceDistricts will change when you switch the city
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> 1094b <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】
Get data from address.plist,and save it as NSDictionary in the local2.The addressPickView will load all provinces, we choose the first city and the first district of the city as default
Realize the func of UIPickerViewDelegate and UIPickerViewDataSource,refresh the addressPickView by reloadComponent()
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 blog http://blog.csdn.net/nc_jingjing/article/details/62444922My technical exchange qq group:457156158
My weibo http://weibo.com/2397939444/profile?topnav=1&wvr=6&is_all=1
My Wechat public number :ITNotesShare
相关文章推荐
- Swift - 选择框(UIPickerView)的用法
- iPhone开发之UITableView的小试牛刀 利用MVC模式从plist文件读取数据并字典转模型的格式实现
- IOS:UIPickerView设置三组数据联动的选择框(地区,上课时间段等类型数据的选择)
- Swift - 选择框(UIPickerView)的用法
- 数据选择控件-UIPickerView的总结
- UI :使用 UIPickerView 来选择数据
- IOS使用视图控制器和视图(三)通过 使用 UIPickerView 来选择数据
- TCP/IP学习笔记二:地址解析协议ARP 数据格式
- UIPickerView数据联动时越界错误的解决方法
- 希望大牛讨论 树状数据如何选择数据库和设计存储格式
- Restful POST请求提交json或www-form-urlencoded数据格式的选择
- 选择什么格式的磁盘分区才能让我的数据更安全一些呢?
- 使用 UITableView 创建表格应用演练(2)——从plist文件加载并显示数据
- excel vba 快速显示及隐藏所选数据行,并获取多重选择区地址的行列号
- (3) iphone 开发 从应用程序看UITableView的:分组,分区,索引,工作原理及其变换法则,plist文件数据定义规则
- iOS-中国省市区plist(续)UIPickerView的应用
- 用WebView访问证书有问题的SSL网页以及在文件关联窗口中选择浏览器后,浏览器如何识别到该连接地址
- Web开发-数据格式选择
- Felomeng翻译:libsvm2.88之附加工具:子取样器,参数选择器,数据格式较验器等