iOS 利用UIPickerView 实现 省市区 联动 swift

iOS 利用UIPickerView 实现 省市区 联动 swift,第1张

效果图

直接上代码

class Test1ViewController: BaseViewController,UIPickerViewDelegate,UIPickerViewDataSource {
    var provinceRow = 0
    var cityRow = 0
    var areaRow = 0
    
    var province = ""
    var city = ""
    var area = ""
    
    
    lazy var dataArray : [[String:Any]] = {
        let path = Bundle.main.path(forResource: "province_city_areas", ofType: "plist")
        let url = URL(fileURLWithPath: path!)
        let array = NSArray(contentsOf: url)!
        return array as! [[String:Any]]
      }()
   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addUIPickerView()
    }
     
    // 添加选择器控件
    func addUIPickerView() {
        // 创建 UIPickerView
        let pickerView = UIPickerView(frame: CGRect(x: 10, y: 100, width: 350, height: 250))
        // 设置 UIPickerView 代理
        pickerView.delegate = self
        // 设置 UIPickerView 数据源
        pickerView.dataSource = self

        view.addSubview(pickerView)
        
        // 主动选中 指定组的指定行,带动画效果
//        pickerView.selectRow(1, inComponent: 2, animated: true)
    }
    

    
    // 返回选择器每个分组的行数
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if component == 0 {
            return dataArray.count
        }
        else if component == 1 {
            let dic = dataArray[provinceRow]
            let cityList = dic["cityList"] as! NSArray
            return cityList.count
        }
        else {
            let dic = dataArray[provinceRow]
            let cityList = dic["cityList"] as! NSArray
            let dict = cityList[cityRow] as! NSDictionary
            let areaList = dict["areaList"] as! NSArray
            return areaList.count
        }
    }
    
    // 返回选择器分组数
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }
    
    // 返回选择器每个分组中每行数据的标题 
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        print("第\(component + 1)组,第\(row + 1)行")
//    }
    
    // 设置图片内容视图
    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let contentView = UIView()
        contentView.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
        //背景图
        let imgView = UIImageView(image: UIImage(named: "health_uricacid_bg"))
        imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
        contentView.addSubview(imgView)
        //名称
        let titleL = UILabel()
        titleL.frame = CGRect(x: 0, y: 0, width: 100, height: 50)
        titleL.text = ""
        titleL.textAlignment = .center
        titleL.textColor = .black
        titleL.backgroundColor = .clear
        contentView.addSubview(titleL)
 
        if component == 0 {
            let dic = dataArray[row]
            titleL.text = "\(dic["name"]!)"
        }
        else if (component == 1) {
            let dic = dataArray[provinceRow]
            let cityList = dic["cityList"] as! NSArray
            let cityDic = cityList[row] as! NSDictionary
            titleL.text = "\(cityDic["name"]!)"
        }
        else {
            let dic = dataArray[provinceRow]
            let cityList = dic["cityList"] as! NSArray
            let cityDic = cityList[cityRow] as! NSDictionary
            let areaList = cityDic["areaList"] as!NSArray
            let areaDic = areaList[row] as! NSDictionary
            titleL.text = "\(areaDic["name"]!)"
        }
        return contentView
    }
    
    // UIPickerView 选中事件监听
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
       if component == 0 {
            provinceRow = row
            cityRow = 0
            let provinceDic = dataArray[row]
            let cityList = provinceDic["cityList"] as! NSArray
            let cityDic = cityList[0] as! NSDictionary
            let areaList = cityDic["areaList"] as! NSArray
            let areaDic = areaList[0] as! NSDictionary
            province = provinceDic["name"] as! String
            city = cityDic["name"] as! String
            area = areaDic["name"] as! String
            pickerView.reloadAllComponents()
            pickerView.selectRow(0, inComponent: 1, animated: true)//市自动跳转到第一行
        }
        else if component == 1 {
            cityRow = row
            areaRow = 0
            let provinceDic = dataArray[provinceRow]
            let cityList = provinceDic["cityList"] as! NSArray
            let cityDic = cityList[row] as! NSDictionary
            let areaList = cityDic["areaList"] as! NSArray
            let areaDic = areaList[0] as! NSDictionary
            city = cityDic["name"] as! String
            area = areaDic["name"] as! String
            pickerView.reloadComponent(2)
            pickerView.selectRow(0, inComponent: 2, animated: true)
        }
        else if component == 2 {
            areaRow = row
            let provinceDic = dataArray[provinceRow]
            let cityList = provinceDic["cityList"] as! NSArray
            let cityDic = cityList[cityRow] as! NSDictionary
            let areaList = cityDic["areaList"] as! NSArray
            let areaDic = areaList[row] as! NSDictionary
            area = areaDic["name"] as! String
        }
        
        print("\(province)-\(city)-\(area)")
        
    }
    
    // 设置 UIPickerView 每个组的宽度
    func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
        if (component == 0) {
            return 100
        } else if (component == 1) {
            return 100
        } else {
            return 100
        }
    }
    
    // 设置 UIPickerView 每组每行的高度
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 60
    }
    
 
}

省市区的plist想要的可以私信我

end

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/994546.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存