无故事王国
2023-10-16 54e863346e9c246d199c7684e506de4f7437b3bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
    //
    //  CityChooseSubTypeView.swift
    //  WanPai
    //
    //  Created by 杨锴 on 2023/6/13.
    //
 
import UIKit
import JQTools
import RxSwift
 
class CityChooseSubTypeView: UIView,JQNibView{
    @IBOutlet weak var cons_viewHeight: NSLayoutConstraint!
    @IBOutlet weak var label_city: UILabel!
    @IBOutlet weak var tableView: UITableView!
    private var closeClouse:(()->Void)?
    private var clouse:((CityProfileModel)->Void)?
    private var disposeBag = DisposeBag()
    private var datas = [CityProfileModel]()
    private var dataDict = Dictionary<String,[CityProfileModel]>()
    private var keys = [String]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        cons_viewHeight.constant = 100
        alpha = 0
        layoutIfNeeded()
        tableView.delegate = self
        tableView.dataSource = self
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0
        } else {
 
        }
        tableView.separatorStyle = .none
        tableView.sectionIndexColor = Def_ThemeColor
//        tableView.register(UINib(nibName: "CommonSingleTCell", bundle: nil), forCellReuseIdentifier: "_CommonSingleTCell")
        startLocation()
        Services.queryAllCity().subscribe(onNext: {[weak self] data in
            guard let weakSelf = self else { return }
            if let models = data.data{
                self?.datas = models
                for v in models{
                    if let py = v.name.jq_getPinyin().first?.uppercased(){
                        if self?.dataDict[py] == nil{
                            self?.dataDict[py] = [CityProfileModel]()
                        }
                        self?.dataDict[py]?.append(v)
                    }
                }
                self?.keys = self?.dataDict.keys.sorted() ?? []
 
 
                let finalHei = JQ_ScreenH - 175  - UIDevice.jq_safeEdges.top - JQ_NavBarHeight
                let ecxclueHei = Double(weakSelf.dataDict.values.count) * 46.0 + Double(weakSelf.keys.count) * 35.0 + 50
                self?.cons_viewHeight.constant = min(finalHei, ecxclueHei)
                self?.layoutIfNeeded()
                self?.tableView.reloadData()
            }
        }).disposed(by: disposeBag)
    }
 
    @discardableResult
    static func show(inView:UIView,afterView:UIView,clouse:@escaping (CityProfileModel)->Void,closeClouse:@escaping ()->Void)->CityChooseSubTypeView{
        let subTypeView = CityChooseSubTypeView.jq_loadNibView()
        subTypeView.closeClouse = closeClouse
        subTypeView.clouse = clouse
        inView.addSubview(subTypeView)
        subTypeView.snp.makeConstraints { make in
            make.top.equalTo(afterView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        UIView.animate(withDuration: 0.2) {
            subTypeView.alpha = 1
        } completion: { _ in
//            subTypeView.cons_viewHeight.constant = JQ_ScreenH - 175  - UIDevice.jq_safeEdges.top - JQ_NavBarHeight
            UIView.animate(withDuration: 0.2) {
                subTypeView.layoutIfNeeded()
            }
        }
        return subTypeView
    }
    
    
    @IBAction func closeAction(_ sender: UIButton) {
        self.cons_viewHeight.constant = 100
        UIView.animate(withDuration: 0.2) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
            self.closeClouse!()
        }
    }
 
    private func startLocation(){
        locationTool.startLocation { location in
            locationTool.geocoder(location) { [weak self] marks, error in
                self?.label_city.text = marks?.last?.locality ?? "定位失败"
                locationTool.stopLocation()
            }
        } errorClouse: { error in
            locationTool.stopLocation()
        }
    }
 
    @IBAction func reLocationAction(_ sender: Any) {
        startLocation()
    }
}
 
extension CityChooseSubTypeView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let key = keys[indexPath.section]
        let model = dataDict[key]![indexPath.row]
        clouse!(model)
 
        self.cons_viewHeight.constant = 100
        UIView.animate(withDuration: 0.2) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
 
}
 
extension CityChooseSubTypeView:UITableViewDataSource{
 
    func sectionIndexTitles(for tableView: UITableView) -> [String]? {
        return keys
    }
 
//    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
//        return keys[section]
//    }
 
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header")
        if header == nil{
            header = UITableViewHeaderFooterView(reuseIdentifier: "header")
            let label = UILabel()
            label.tag = 1000
            label.text = keys[section]
            label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            label.textColor = .black.withAlphaComponent(0.79)
            header?.contentView.addSubview(label)
            label.snp.makeConstraints { make in
                make.left.equalTo(14)
                make.centerY.equalToSuperview()
            }
        }
            if let label = header?.contentView.viewWithTag(1000) as? UILabel{
                label.text = keys[section]
            }
        return header
    }
 
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 35
    }
 
    func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
 
        return index
    }
 
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 46
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "_citySingleTCell")
        if cell == nil{
            cell = UITableViewCell(style: .default, reuseIdentifier: "_citySingleTCell")
            let label = UILabel()
            label.tag = 1000
            label.text = keys[indexPath.section]
            label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
            label.textColor = .black.withAlphaComponent(0.80)
            cell?.contentView.addSubview(label)
            label.snp.makeConstraints { make in
                make.left.equalTo(14)
                make.centerY.equalToSuperview()
            }
        }
 
 
        if let label = cell!.contentView.viewWithTag(1000) as? UILabel{
            if let model = dataDict[keys[indexPath.section]]?[indexPath.row]{
                label.text = model.name
            }
        }
 
        return cell!
    }
 
    func numberOfSections(in tableView: UITableView) -> Int {
        return keys.count
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataDict[keys[section]]?.count ?? 0
    }
}