This article mainly introduces the basic usage of UIPickerView, we can obtain address information by selecting the provinces ,cities and districts.
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>
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)
}
}
}
http://download.csdn.net/detail/nc_jingjing/9788521