| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- //
- // 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
- }
- }
|