首页 > 代码库 > swift3.0 三级联动UIPickerView城市选择器
swift3.0 三级联动UIPickerView城市选择器
初学swift没几天,就试着自己写了一个城市选择器,纯swift代码。
ViewController.swift文件中:
//
// ViewController.swift
// swift demo - UIPickerView之城市选择器
//
// Created by 柯其谱 on 17/3/11.
// Copyright ? 2017年 柯其谱. All rights reserved.
//
import UIKit
//MARK: View life cycle
class ViewController: UIViewController {
/** 懒加载的城市数据源数组 */
fileprivate lazy var cCityDataArray: [StateModel] = {
//Plist文件路径
let path = Bundle.main.path(forResource: "area", ofType: "plist")
var areas: NSArray? = nil
//读取Plist文件数组
if let arrayPath = path {
areas = NSArray (contentsOfFile: arrayPath)
}
//数组转换为模型数组
var cityData = http://www.mamicode.com/Array()"请选择城市"
cityTextField.borderStyle = UITextBorderStyle.roundedRect
self.view.addSubview(cityTextField)
}
}
/** 设置背景蒙版Button */
private func setupCoverButton() -> Void {
cTextFieldCoverButton.addTarget(self, action: #selector(clickTextFieldCoverButton(sender:)), for: UIControlEvents.touchUpInside)
self.view.addSubview(cTextFieldCoverButton)
cTextFieldCoverButton.isHidden = true
}
/** 设置pickerView */
private func setupPickerView() -> Void {
if let cityPickerView = cCityPickerView {
cityPickerView.delegate = self
cityPickerView.dataSource = self
cCityTextField!.inputView = cityPickerView
}
}
/** 设置toolBar */
private func setupToolBar() -> Void {
if let cityToolBar = cCityToolBar {
cityToolBar.frame = CGRect (x: 0, y: 0, width: 0, height: 44)
cityToolBar.backgroundColor = UIColor.white
let cancelItem = UIBarButtonItem.init(title: "取消", style: UIBarButtonItemStyle.done, target: self, action: #selector(clickToolBarCancelItem(sender:)))
let spaceItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
let doneItem = UIBarButtonItem.init(title: "确定", style: UIBarButtonItemStyle.done, target: self, action: #selector(clickToolBarDoneItem(sender:)))
cityToolBar.items = [cancelItem, spaceItem, doneItem]
cCityTextField?.inputAccessoryView = cityToolBar
}
}
}
//MARK: UITextFieldDelegate
extension ViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
cTextFieldCoverButton.isHidden = false
}
func textFieldDidEndEditing(_ textField: UITextField) {
cTextFieldCoverButton.isHidden = true
}
}
//MARK: UIPickerViewDelegate
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch component {
case 0:
return cStatesArr!.count
case 1:
return cCitiesArr!.count
case 2:
return cAreasArr!.count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch component {
case 0:
return cStatesArr![row].state
case 1:
return cCitiesArr![row].city
case 2:
return cAreasArr![row]
default:
return nil
}
}
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return self.view.frame.width / 3
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch component {
case 0:
//保存市级数据源数组
cCitiesArr = cCityDataArray[row].cities
//若有区级数据,则保存区级数据源数组
if cCitiesArr?.first?.areas != nil {
cAreasArr = cCitiesArr?.first?.areas
}
//重新加载第1、2组
pickerView.reloadComponent(1)
pickerView.reloadComponent(2)
//默认选中第1、2组的第1行数据
pickerView.selectRow(0, inComponent: 1, animated: true)
pickerView.selectRow(0, inComponent: 2, animated: true)
case 1:
//保存区级数据源数组
if cCitiesArr![row].areas.count != 0 {
cAreasArr = cCitiesArr![row].areas
}
//重新加载第2组数据
pickerView.reloadComponent(2)
//默认选中第2组第1行数据
pickerView.selectRow(0, inComponent: 2, animated: true)
default:
return
}
}
}
//MARK: Event response
extension ViewController {
@objc fileprivate func clickTextFieldCoverButton(sender: UIButton) -> Void {
cCityTextField?.resignFirstResponder()
}
@objc fileprivate func clickToolBarCancelItem(sender: UIBarButtonItem) -> Void {
cCityTextField?.resignFirstResponder()
}
@objc fileprivate func clickToolBarDoneItem(sender: UIBarButtonItem) -> Void {
cCityTextField?.resignFirstResponder()
let selectedState = cCityPickerView?.selectedRow(inComponent: 0)
let selectedCity = cCityPickerView?.selectedRow(inComponent: 1)
let selectedArea = cCityPickerView?.selectedRow(inComponent: 2)
var cityString = (cStatesArr?[selectedState!].state)! + (cCitiesArr?[selectedCity!].city)!
if cAreasArr?.count != 0 {
cityString += (cAreasArr?[selectedArea!])!
}
cCityTextField?.text = cityString
}
}
StateModel.swfit文件中,用于保存省级数据:
//
// StateModel.swift
// swift demo - UIPickerView之城市选择器
//
// Created by 柯其谱 on 17/3/11.
// Copyright ? 2017年 柯其谱. All rights reserved.
//
import UIKit
/** 省级Model */
class StateModel: NSObject {
/** 省下级所有城市 */
let cities: Array<CityModel>
/** 省级名称 */
let state: String
init(dictionary: NSDictionary) {
state = dictionary["state"] as! String
let citiesArr = dictionary["cities"] as! NSArray
var citiesMutableArr = Array<CityModel>()
for city in citiesArr {
let cityDic = city as! NSDictionary
let cityModel = CityModel.init(dictionary: cityDic)
citiesMutableArr.append(cityModel)
}
cities = citiesMutableArr
}
}
CityModel.swift文件中,用于保存市级数据
//
// CityModel.swift
// swift demo - UIPickerView之城市选择器
//
// Created by 柯其谱 on 17/3/11.
// Copyright ? 2017年 柯其谱. All rights reserved.
//
import UIKit
//MARK: 城市Model
class CityModel: NSObject {
/** 城市下级area */
let areas: Array<String>
/** 城市名 */
let city: String
init(dictionary: NSDictionary){
city = dictionary["city"] as! String
let areasArr = dictionary["areas"] as! NSArray
var areasMutableArr = Array<String>()
for area in areasArr {
let areaString = area as! String
areasMutableArr.append(areaString)
}
areas = areasMutableArr
}
}
swift3.0 三级联动UIPickerView城市选择器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。