本文主要是介绍ios/swift 之省市区三级联动的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
省市区乡镇联动:https://github.com/artiely/Administrative-divisions-of-China
1.*********省市区三级联动*********
/**UIPickView、省市区需要注意的问题:同时滑动两列崩溃,解决办法:判断后执行使用:let provincepick = LYBUIPickView.init(frame: CGRect(x: 10, y: 0, width: WIDTH, height: 300))provincepick.provinceCityAreaBlock={(province,city,area)inprint("\(province)---\(city)---\(area)")}view.addSubview(provincepick)*/
import UIKitclass LYBUIPickView: UIView,UIPickerViewDelegate,UIPickerViewDataSource {var pickerView:UIPickerView!var modelArr:[LYBprovinceModel]?//所有数据的模型数组var provincemodel:LYBprovinceModel?//省模型var citymodel:LYBCityModel?//市模型var provinceStr:String?=""//省var cityStr:String?=""//市var area:String?=""//区//定义一个闭包把省市区数据传出去var provinceCityAreaBlock:(_ province:String,_ city:String,_ area:String)->()={(province:String,city:String,area:String)->()in}override init(frame: CGRect) {super.init(frame:frame)createDatePickview(frame: frame)}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}//确认或取消按钮@objc func sureOrcanceBtnClick(sender:UIButton){switch sender.tag {case 20://取消self.removeFromSuperview()breakcase 21:self.provinceCityAreaBlock(provinceStr!,cityStr!,area!)self.removeFromSuperview()breakdefault: break}}//解析省市区数据func parseDate(){let path=Bundle.main.path(forResource: "shengshiquxianarea.plist", ofType: nil)let proviceDataArr:NSArray = NSArray(contentsOfFile:path!)!modelArr=([LYBprovinceModel].deserialize(from: proviceDataArr) as! [LYBprovinceModel])
// print("\(modelArr?.first?.cities?.first?.areas)")}//设置默认的省市区字符串func setDefaultProvinceCityArea(){provincemodel=modelArr![0]//默认选中第一个省的模型provinceStr=provincemodel?.state//省名称if((provincemodel?.cities?.count)!>0){cityStr=provincemodel?.cities?.first?.city//城市,默认先选第一个if((provincemodel?.cities?.first?.areas?.count)!>0){area = provincemodel?.cities?.first?.areas?.first//区,默认先选第一个}}}func createDatePickview(frame: CGRect){parseDate()setDefaultProvinceCityArea()//设置默认的省市区//取消和确定按钮let buttonView=UIView.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH, height: 50))addSubview(buttonView)let cancelBtn=UIButton.init(frame: CGRect.init(x: 20, y: 0, width: 100, height: 50))cancelBtn.setTitle("取消", for: UIControl.State.normal)cancelBtn.tag=20cancelBtn.addTarget(self, action: #selector(sureOrcanceBtnClick), for: UIControl.Event.touchUpInside)buttonView.addSubview(cancelBtn)let sureBtn=UIButton.init(frame: CGRect.init(x: WIDTH-150, y: 0, width: 100, height: 50))sureBtn.setTitle("确定", for: UIControl.State.normal)sureBtn.tag=21sureBtn.addTarget(self, action: #selector(sureOrcanceBtnClick), for: UIControl.Event.touchUpInside)buttonView.addSubview(sureBtn)//初始化UIPickerViewpickerView = UIPickerView.init(frame: CGRect(x:0, y:50, width:WIDTH, height:frame.size.height-50))pickerView.dataSource = selfpickerView.delegate = self//设置选择框的默认值pickerView.selectRow(0,inComponent:0,animated:true)addSubview(pickerView)}//设置选择框的列数为3列,继承于UIPickerViewDataSource协议func numberOfComponents(in pickerView: UIPickerView) -> Int {return 3}//设置选择框的行数,继承于UIPickerViewDataSource协议func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {if component==0 {return (modelArr?.count)!}else if(component==1){let provincenum=pickerView.selectedRow(inComponent: 0)//第一列选中的行号provincemodel=modelArr![provincenum]//选中的省的模型return (provincemodel!.cities?.count)!}else if(component==2){let citynum=pickerView.selectedRow(inComponent: 1)//第2列选中的行号if((provincemodel?.cities?.count)!>citynum){citymodel=provincemodel?.cities![citynum]//选中的城市模型}return (citymodel?.areas?.count)!}return 0}//设置选择框各显示的内容,继承于UIPickerViewDelegate协议func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,forComponent component: Int) -> String? {if(component==0){provincemodel=modelArr![row]return provincemodel?.state}else if(component==1){if((provincemodel?.cities?.count)!>row){citymodel=provincemodel?.cities![row]//选中的城市模型}return citymodel!.city}else if(component==2){if(citymodel!.areas!.count>row){return citymodel!.areas![row];}}return ""}// //自定义返回每一行返回的内容,如果设置了这个自定义的,上面的titlefor view就被覆盖了// func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {//// return UIView.init()// }//选中某一行func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {if component==0 {let provinceIndex=pickerView.selectedRow(inComponent: 0)//第一列选中的行号provincemodel=modelArr![provinceIndex]//选中的省的模型provinceStr=provincemodel?.state//省if((provincemodel?.cities?.count)!>0){pickerView.reloadComponent(1)pickerView.selectRow(0, inComponent: 1, animated: true)cityStr=provincemodel?.cities?.first?.city//城市,默认先选第一个area=""//如果是澳门国外等没有第三列,在这里置为空,要不然就会显示最有一个数据if((provincemodel?.cities?.first?.areas?.count)!>0){area = provincemodel?.cities?.first?.areas?.first//区,默认先选第一个}pickerView.reloadComponent(2)}}else if(component==1){let citynum=pickerView.selectedRow(inComponent: 1)//第1列选中的行号citymodel=provincemodel?.cities![citynum]//选中的城市模型cityStr=citymodel?.city//选中的城市pickerView.reloadComponent(2)area=""//如果是澳门国外等没有第三列,在这里置为空,要不然就会显示最有一个数据if((citymodel?.areas?.count)!>0){area=citymodel?.areas?.firstpickerView.selectRow(0, inComponent: 2, animated: true)}}else if(component==2){let areaIndx=pickerView.selectedRow(inComponent: 2)//第2列选中的行号let citynum=pickerView.selectedRow(inComponent: 1)//第1列选中的行号citymodel=provincemodel?.cities![citynum]//选中的城市模型if((citymodel?.areas?.count)!>0){area=citymodel?.areas![areaIndx]}}self.provinceCityAreaBlock(provinceStr!,cityStr!,area!)
// print("\(provinceStr)---\(cityStr)---\(area)")}}********下面是模型*****
/**省的模型*/import UIKitclass LYBprovinceModel:HandyJSON {var state:String?//省var cities:[LYBCityModel]?//城市//必须加上下面的方法required init(){}
}*********
/**城市*/
import UIKitclass LYBCityModel:HandyJSON {var city:String?// 城市var areas:[String]?//区、县级市//必须加上下面的方法required init(){}
}******
/**区、县模型,这个个暂时用不到*/
import UIKitclass LYBAreaModel:HandyJSON{var area:String?//区、县级市//必须加上下面的方法required init(){}
}
这篇关于ios/swift 之省市区三级联动的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!