GeographicPickView.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // GeographicPickView.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/11/9.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. /// 省市区选择器视图容器控制类
  10. class GeographicPickView: UIViewController {
  11. // MARK: - 属性列表
  12. /// 取消按钮
  13. @IBOutlet weak var cancel: UIButton!
  14. /// 确定按钮
  15. @IBOutlet weak var sure: UIButton!
  16. /// 查看全部
  17. @IBOutlet weak var all: UIButton!
  18. /// 结果标签
  19. @IBOutlet weak var reault: UILabel!
  20. /// 结果标签
  21. @IBOutlet weak var pickView: UIPickerView!
  22. /// 地址信息
  23. var geographics: [MoGeographicPosition] = [] {
  24. didSet {
  25. if geographics.count != 0 {
  26. /// 刷新列表
  27. pickView.reloadComponent(0)
  28. if geographics[0].Cities?.count != 0 {
  29. /// 默认选中第一个
  30. self.citys = geographics[0].Cities
  31. self.city = geographics[0].Cities?[0]
  32. } else { /// 默认显示省份
  33. self.citys = []
  34. self.city = geographics[0].Province
  35. }
  36. }
  37. }
  38. }
  39. var citys: [MoGeographic]? {
  40. didSet {
  41. /// 刷新列表
  42. pickView.reloadComponent(1)
  43. }
  44. }
  45. /// 选中的数据信息
  46. var city: MoGeographic? {
  47. didSet {
  48. /// 更新结果显示
  49. self.reault.text = self.city?.pathname
  50. }
  51. }
  52. /// 创建block变量
  53. var callback: ((_ takeInfo: MoGeographic?, _ isSure: Bool) ->())?
  54. // MARK: - 生命周期相关
  55. override func viewDidLoad() {
  56. super.viewDidLoad()
  57. // Do any additional setup after loading the view.
  58. self.geographics = MTP2BusinessCore.shared.accountManager?.geographics ?? []
  59. }
  60. // MARK: - 交互相关
  61. @IBAction func onButtonPressed(_ sender: UIButton) {
  62. switch sender {
  63. case cancel: /// 取消按钮
  64. self.dismiss(animated: true) {
  65. if self.callback != nil { self.callback!(self.city, false) }
  66. }
  67. case all:
  68. self.dismiss(animated: true) {
  69. if self.callback != nil { self.callback!(nil, false) }
  70. }
  71. default: /// 确定按钮
  72. self.dismiss(animated: true) {
  73. if self.callback != nil { self.callback!(self.city, true) }
  74. }
  75. break
  76. }
  77. }
  78. /// onTapGestureRecognizer
  79. /// - Parameter sender: sender
  80. @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
  81. self.dismiss(animated: true) {
  82. if self.callback != nil { self.callback!(self.city, false) }
  83. }
  84. }
  85. /*
  86. // MARK: - Navigation
  87. // In a storyboard-based application, you will often want to do a little preparation before navigation
  88. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  89. // Get the new view controller using segue.destination.
  90. // Pass the selected object to the new view controller.
  91. }
  92. */
  93. }
  94. // MARK: - UIPickerViewDelegate, UIPickerViewDataSource
  95. extension GeographicPickView: UIPickerViewDelegate, UIPickerViewDataSource {
  96. /// numberOfComponents
  97. /// - Parameter pickerView: pickerView
  98. /// - Returns: Int
  99. func numberOfComponents(in pickerView: UIPickerView) -> Int {
  100. return 2
  101. }
  102. func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  103. if component == 0 {
  104. return geographics.count
  105. } else {
  106. return citys?.count ?? 0
  107. }
  108. }
  109. func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
  110. if component == 0 {
  111. return geographics[row].Province?.divisionname.withTextColor(UIColorFromHex(rgbValue: 0x6B6177)).withFont(.font_12)
  112. } else {
  113. return citys?[row].divisionname.withTextColor(UIColorFromHex(rgbValue: 0x6B6177)).withFont(.font_12)
  114. }
  115. }
  116. func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  117. if component == 0 {
  118. self.citys = geographics[row].Cities
  119. pickView.selectRow(0, inComponent: 1, animated: true)
  120. if self.citys?.count != 0 {
  121. self.city = self.citys?[0]
  122. } else { /// 如果为空则默认第一个省份
  123. self.city = geographics[row].Province
  124. }
  125. } else {
  126. if self.citys?.count != 0 {
  127. self.city = self.citys?[row]
  128. }
  129. }
  130. }
  131. func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
  132. return 44.0
  133. }
  134. }