// // GeographicPickView.swift // MTP2_iOS // // Created by Handy_Cao on 2020/11/9. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit /// 省市区选择器视图容器控制类 class GeographicPickView: UIViewController { // MARK: - 属性列表 /// 取消按钮 @IBOutlet weak var cancel: UIButton! /// 确定按钮 @IBOutlet weak var sure: UIButton! /// 查看全部 @IBOutlet weak var all: UIButton! /// 结果标签 @IBOutlet weak var reault: UILabel! /// 结果标签 @IBOutlet weak var pickView: UIPickerView! /// 地址信息 var geographics: [MoGeographicPosition] = [] { didSet { if geographics.count != 0 { /// 刷新列表 pickView.reloadComponent(0) if geographics[0].Cities?.count != 0 { /// 默认选中第一个 self.citys = geographics[0].Cities self.city = geographics[0].Cities?[0] } else { /// 默认显示省份 self.citys = [] self.city = geographics[0].Province } } } } var citys: [MoGeographic]? { didSet { /// 刷新列表 pickView.reloadComponent(1) } } /// 选中的数据信息 var city: MoGeographic? { didSet { /// 更新结果显示 self.reault.text = self.city?.pathname } } /// 创建block变量 var callback: ((_ takeInfo: MoGeographic?, _ isSure: Bool) ->())? // MARK: - 生命周期相关 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. self.geographics = MTP2BusinessCore.shared.accountManager?.geographics ?? [] } // MARK: - 交互相关 @IBAction func onButtonPressed(_ sender: UIButton) { switch sender { case cancel: /// 取消按钮 self.dismiss(animated: true) { if self.callback != nil { self.callback!(self.city, false) } } case all: self.dismiss(animated: true) { if self.callback != nil { self.callback!(nil, false) } } default: /// 确定按钮 self.dismiss(animated: true) { if self.callback != nil { self.callback!(self.city, true) } } break } } /// onTapGestureRecognizer /// - Parameter sender: sender @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) { self.dismiss(animated: true) { if self.callback != nil { self.callback!(self.city, false) } } } /* // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. } */ } // MARK: - UIPickerViewDelegate, UIPickerViewDataSource extension GeographicPickView: UIPickerViewDelegate, UIPickerViewDataSource { /// numberOfComponents /// - Parameter pickerView: pickerView /// - Returns: Int func numberOfComponents(in pickerView: UIPickerView) -> Int { return 2 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if component == 0 { return geographics.count } else { return citys?.count ?? 0 } } func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { if component == 0 { return geographics[row].Province?.divisionname.withTextColor(UIColorFromHex(rgbValue: 0x6B6177)).withFont(.font_12) } else { return citys?[row].divisionname.withTextColor(UIColorFromHex(rgbValue: 0x6B6177)).withFont(.font_12) } } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if component == 0 { self.citys = geographics[row].Cities pickView.selectRow(0, inComponent: 1, animated: true) if self.citys?.count != 0 { self.city = self.citys?[0] } else { /// 如果为空则默认第一个省份 self.city = geographics[row].Province } } else { if self.citys?.count != 0 { self.city = self.citys?[row] } } } func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat { return 44.0 } }