swift4.0 UIPickerView的使用(省市區(qū)、日期、性別選擇器)

swift4.0 省市區(qū)、日期、性別選擇器

一、swift日期選擇器很簡(jiǎn)單


? ? ? ? ? ? ? ?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)

三、地址選擇器


首先讀取本地省、市、區(qū)文件

? ? ? ? ? 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)

然后執(zhí)行UIPickerView的協(xié)議代理:

? /// 返回列

?? ? ///

?? ? /// -ParameterpickerView: pickerView

?? ? /// -Returns: 列

?? ? funcnumberOfComponents(in pickerView:UIPickerView) ->Int{

? ? ? ? ? switch isAddress{

? ? ? ? ? case true:

?? ? ? ? ? ? ? return 3

? ? ? ? ? default:

?? ? ? ? ? ? ? return 1

? ? ? ? ? }

?? ? }


?? ? /// 返回對(duì)應(yīng)列的行數(shù)

?? ? ///

?? ? /// -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

? ? ? ? ? }

?? ? }


?? ? /// 返回對(duì)應(yīng)行的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)

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? // 默認(rèn)選擇當(dāng)前省的第一個(gè)城市對(duì)應(yīng)的區(qū)縣

? ? ? ? ? ? ? ? ? ? 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

? ? ? ? ? }

?? ? }


代碼地址:https://github.com/bhjmoshang/BHJPickerView.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Another way to recycle plastic Plastic production has tri...
    Berry521閱讀 228評(píng)論 0 0
  • 鑒于在搭建時(shí),參考網(wǎng)上很多資料,網(wǎng)上資料在有用的同時(shí),也坑了很多人 本文的目的,也就是想讓后繼之人在搭建svn服務(wù)...
    52d3a1968d95閱讀 355評(píng)論 0 0
  • 本來(lái)該今天預(yù)產(chǎn)期的笑笑豬,提前了19天來(lái)到我身邊,可能上天派他來(lái)渡我的吧,他讓我自己學(xué)會(huì)調(diào)整心情,調(diào)整心態(tài),就算他...
    小笨豬520閱讀 158評(píng)論 0 0
  • 事務(wù) //實(shí)際上動(dòng)畫(huà)執(zhí)行的時(shí)間取決于當(dāng)前事務(wù)的設(shè)置,動(dòng)畫(huà)類型取決于圖層行為。//事務(wù)實(shí)際上是Core Animat...
    S大偉閱讀 228評(píng)論 0 0
  • “媽,我昨天晚上做了個(gè)特別詭異的夢(mèng)——整個(gè)學(xué)校的人在樓道走廊里玩悠悠球,我在他們面前把手一抬,全部人的悠悠球都突然...
    Loopooi閱讀 755評(píng)論 2 7

友情鏈接更多精彩內(nèi)容