swift4.0 省市区、日期、性别选择器
一、swift日期选择器很简单
datePicker.datePickerMode = UIDatePickerMode.date datePicker=UIDatePicker.init(frame:CGRect.init(x:0, y:44, width:kScreenWidth, height:pickerH-44))
datePicker.locale=Locale.init(identifier:"zh_CN")
datePicker.backgroundColor = UIColor.white
datePicker.addTarget(self, action:#selector(BHJPickerView.dateSelected(_:)), for:UIControlEvents.valueChanged)
datePicker.setDate(Date(), animated:true)
二、性别选择器
genderPicker=UIPickerView.init(frame:CGRect.init(x:0, y:44, width:kScreenWidth, height:pickerH-44))
genderPicker.delegate=self
genderPicker.dataSource=self
genderPicker.backgroundColor = UIColor.white
dataArray=NSMutableArray.init(array: ["男","女"])
self.pickerView(genderPicker, didSelectRow:0, inComponent:0)
三、地址选择器
首先读取本地省、市、区文件
let path =Bundle.main.path(forResource:"city", ofType:"json")
let url =URL(fileURLWithPath: path!)
let addressData =NSData.init(contentsOf: url)
let addressDic = try! JSONSerialization.jsonObject(with: addressData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray
let dic = addressDic.object(at:0)as!NSDictionary
let provinceArray = dic["childs"]as!NSArray
for I in0..<provinceArray.count
let provinceDic = provinceArray.object(at: i)as!NSDictionary
let provinceM =AddressModel.init()
provinceM.region_name= (provinceDic["region_name"]as?String)
provinceM.region_id= (provinceDic["region_id"]as!String)
provinceM.agency_id= (provinceDic["agency_id"]as?String)
provinceM.parent_id= (provinceDic["parent_id"]as!String)
provinceM.region_type= (provinceDic["region_type"]as!String)
provinceM.childs= (provinceDic["childs"]as! [NSDictionary])
self.dataArray.add(provinceM)
然后执行UIPickerView的协议代理:
/// 返回列
///
/// -ParameterpickerView: pickerView
/// -Returns: 列
funcnumberOfComponents(in pickerView:UIPickerView) ->Int{
switch isAddress{
case true:
return 3
default:
return 1
}
}
/// 返回对应列的行数
///
/// -Parameters:
/// - pickerView: pickerView
/// - component: 列
/// -Returns: 行
funcpickerView(_pickerView:UIPickerView, numberOfRowsInComponent component:Int) ->Int{
switch isAddress{
case true:
if component ==0{
return dataArray.count
}elseifcomponent ==1{
return cityArray.count
}else{
return districtArray.count
}
default:
return dataArray.count
}
}
/// 返回对应行的title
///
/// -Parameters:
/// - pickerView: pickerView
/// - row: 行
/// - component: 列
/// -Returns: title
funcpickerView(_pickerView:UIPickerView, titleForRow row:Int, forComponent component:Int) ->String? {
var title =""
switch isAddress{
case true:
if component ==0{
let provinceM =dataArray[row]as!AddressModel
title = provinceM.region_name??"未知"
return title
}else if component ==1{
let cityModel =cityArray[row]as!AddressModel
title = cityModel.region_name??"未知"
return title
}else{
let areaModel =districtArray[row]as!AddressModel
title = areaModel.region_name??"未知"
return title
}
default:
title =dataArray[row]as!String
return title
}
}
/// 选择列、行
///
/// -Parameters:
/// - pickerView: pickerView
/// - row: 行
/// - component: 列
func pickerView(_pickerView:UIPickerView, didSelectRow row:Int, inComponent component:Int) {
switch isAddress{
case true:
if component ==0{
let provinceM =dataArray[row]as!AddressModel
let cityDicArray = provinceM.childs!
cityArray.removeAllObjects()
for j in 0..cityDicArray.count
letcityDic = cityDicArray[j]
letcityM =AddressModel.init()
cityM.region_name= (cityDic["region_name"]as?String)
cityM.region_id= (cityDic["region_id"]as!String)
cityM.agency_id= (cityDic["agency_id"]as?String)
cityM.parent_id= (cityDic["parent_id"]as!String)
cityM.region_type= (cityDic["region_type"]as!String)
cityM.childs= (cityDic["childs"]as! [NSDictionary])
cityArray.add(cityM)
}
// 默认选择当前省的第一个城市对应的区县
self.pickerView(pickerView, didSelectRow:0, inComponent:1)
selectedProvince= provinceM
}else if component ==1{
let cityModel =cityArray[row]as!AddressModel
let areaArray = cityModel.childs!
districtArray.removeAllObjects()
for j in 0..<areaArray.count
letareaDic = areaArray[j]
letareaModel =AddressModel.init()
areaModel.region_name= (areaDic["region_name"]as?String)
areaModel.region_id= (areaDic["region_id"]as!String)
areaModel.agency_id= (areaDic["agency_id"]as?String)
areaModel.parent_id= (areaDic["parent_id"]as!String)
areaModel.region_type= (areaDic["region_type"]as!String)
districtArray.add(areaModel)
}
selectedCity= cityModel
self.pickerView(pickerView, didSelectRow:0, inComponent:2)
}else{
let areaModel =districtArray[row]as!AddressModel
selectedDistrict= areaModel
}
pickerView.reloadAllComponents()
default:
selectedGender=dataArray[row]as!String
}
}