CNLiveDigitalResumeInfoSelectAddressController.swift
18.3 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//
// CNLiveDigitalResumeInfoSelectAddressController.swift
// metaCode
//
// Created by zx on 2024/7/22.
// 数字简历信息-选择地址页面
import Foundation
typealias FinishSelectAreaBlock = () -> Void
class CNLiveDigitalResumeInfoSelectAddressController:CNLiveBaseViewController,UITableViewDelegate, UITableViewDataSource{
var provinceId:String = ""
var province:String = ""
var cityId:String = ""
var city:String = ""
var areaId:String = ""
var area:String = ""
var finishSelectAreaCallBack:FinishSelectAreaBlock?
var shadowView:UIView?
var contentView:UIView?
var line:UIImageView?
var provinceBtn:UIButton?
var cityBtn:UIButton?
var areaBtn:UIButton?
var selectImageView:UIImageView?
lazy var tableView : UITableView = {
let tableView = UITableView(frame: CGRect(x: 0, y: self.line!.bottom, width: self.contentView!.width, height: self.contentView!.height-self.line!.bottom), style: .plain)
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = adapt_length(length: 40)
tableView.showsVerticalScrollIndicator = false
tableView.backgroundColor = .white
tableView.separatorStyle = .none
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: self.contentView!.width, height: adapt_length(length: 5)))
tableView.tableHeaderView = headerView
return tableView
}()
var provinceDataArr:Array<CNLiveAddrAreaModel>?
var cityDataArr:Array<CNLiveAddrAreaModel>?
var areaDataArr:Array<CNLiveAddrAreaModel>?
// MARK: - setter
var _displayLevel:NSInteger?
var displayLevel:NSInteger?{
get{
_displayLevel
}
set{
_displayLevel = newValue
UIView.animate(withDuration: 0.2) {
if (self.displayLevel == 2) {
self.selectImageView!.centerX = self.cityBtn!.centerX
self.cityBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else if (self.displayLevel == 3) {
self.selectImageView!.centerX = self.areaBtn!.centerX
self.areaBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else {
self.selectImageView!.centerX = self.provinceBtn!.centerX
}
}
}
}
//临时选中省
var _temp_selectProvinceModel:CNLiveAddrAreaModel?
var temp_selectProvinceModel:CNLiveAddrAreaModel?{
get{
_temp_selectProvinceModel
}
set{
_temp_selectProvinceModel = newValue
if ((_temp_selectProvinceModel) != nil) {
self.provinceBtn?.setTitle(_temp_selectProvinceModel?.name, for: .normal)
}else {
self.provinceBtn?.setTitle("请选择", for: .normal)
}
}
}
//临时选中市
var _temp_selectCityModel:CNLiveAddrAreaModel?
var temp_selectCityModel:CNLiveAddrAreaModel?{
get{
_temp_selectCityModel
}
set{
_temp_selectCityModel = newValue
if ((_temp_selectCityModel) != nil) {
self.cityBtn?.setTitle(_temp_selectCityModel?.name, for: .normal)
self.cityBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else {
self.cityBtn?.setTitle("城市", for: .normal)
if (self.displayLevel == 2) {
self.cityBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else {
self.cityBtn?.setTitleColor(HexColor("#999999"), for: .normal)
}
}
}
}
//临时选中区(县)
var _temp_selectAreaModel:CNLiveAddrAreaModel?
var temp_selectAreaModel:CNLiveAddrAreaModel?{
get{
_temp_selectAreaModel
}
set{
_temp_selectAreaModel = newValue
if ((_temp_selectAreaModel) != nil) {
self.areaBtn?.setTitle(_temp_selectAreaModel?.name, for: .normal)
self.areaBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else {
self.areaBtn?.setTitle("区县", for: .normal)
if (self.displayLevel == 3) {
self.areaBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else {
self.areaBtn?.setTitleColor(HexColor("#999999"), for: .normal)
}
}
}
}
// MARK: - 显示/隐藏
func display(){
self.view.isUserInteractionEnabled = true
UIView.animate(withDuration: 0.2) {
self.shadowView?.alpha = 1
self.contentView?.y = self.view.height - adapt_length(length: 400)
}
}
func hide(){
UIView.animate(withDuration: 0.2) {
self.shadowView?.alpha = 0
self.contentView?.y = self.view.height
}completion: { finished in
self.view.isUserInteractionEnabled = false
}
}
// MARK: - 数据请求
/**
数据请求
@param pid 父级id
@param level 第几级
*/
func postDataWithPid(pid:String,level:NSInteger,resultBlock: @escaping downSlideResultBlock){
showLoading(message: "")
CNLiveVitaeViewModel.getCityListWithId(id: pid) { result, status in
hiddenLoading()
if status == .success{
self.displayLevel = level
let model = result as! CNLiveAddrAreaListModel
if level == 1{
self.provinceDataArr = model.list
self.temp_selectCityModel = nil
self.temp_selectAreaModel = nil
self.cityDataArr = nil
self.areaDataArr = nil
if self.provinceId.count > 0 && self.provinceId != "0"{
for (_,obj) in self.provinceDataArr!.enumerated(){
if obj.id == self.provinceId{
self.temp_selectProvinceModel = obj
break
}
}
}
}else if level == 2{
self.cityDataArr = model.list
self.temp_selectCityModel = nil
self.temp_selectAreaModel = nil
self.areaDataArr = nil
if self.cityId.count > 0 && self.cityId != "0"{
for (_,obj) in self.cityDataArr!.enumerated(){
if obj.id == self.cityId{
self.temp_selectCityModel = obj
break
}
}
}else{
self.cityBtn?.setTitle("城市", for: .normal)
}
}else if level == 3{
self.areaDataArr = model.list
self.temp_selectAreaModel = nil
if self.areaId.count > 0 && self.areaId != "0"{
for (_,obj) in self.areaDataArr!.enumerated(){
if obj.id == self.areaId{
self.temp_selectAreaModel = obj
break
}
}
}else{
self.areaBtn?.setTitle("区县", for: .normal)
}
}
self.tableView.reloadData()
self.tableView.setContentOffset(CGPointZero, animated: true)
resultBlock(true, .success)
}
}
}
// MARK: - viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .clear
self.topNavView.removeFromSuperview()
self.initUI()
if self.provinceId == "0" || self.provinceId.count == 0{
self.cityId = "0"
self.areaId = "0"
}else if self.cityId == "0" || self.cityId.count == 0{
self.areaId = "0"
}
self.postDataWithPid(pid: "0", level: 1) { result, status in
if status == .success{
if self.cityId.count > 0 && self.cityId != "0"{
self.postDataWithPid(pid: self.provinceId, level: 2){ result, status in
if self.areaId.count > 0 && self.areaId != "0"{
self.postDataWithPid(pid: self.cityId, level: 3) { result, status in
}
}
}
}
}
}
}
func scrollViewWithProvinceId(provinceId:String,cityId:String,areaId:String){
if provinceId == "0"{
self.postDataWithPid(pid: provinceId, level: 1){ result, status in
if areaId.count > 0 && areaId != "0"{
self.postDataWithPid(pid: cityId, level: 2) { result, status in
}
}
}
}else{
self.postDataWithPid(pid: provinceId, level: 2){ result, status in
if areaId.count > 0 && areaId != "0"{
self.postDataWithPid(pid: cityId, level: 3) { result, status in
}
}
}
}
}
// MARK: - initUI
func initUI(){
self.shadowView = UIView(frame: self.view.bounds)
self.shadowView?.backgroundColor = UIColor(white: 0, alpha: 0.26)
self.shadowView?.alpha = 0
self.view.addSubview(self.shadowView!)
let shadowViewTap = UITapGestureRecognizer(target: self, action: #selector(shadowViewTap))
self.shadowView?.addGestureRecognizer(shadowViewTap)
self.contentView = UIView(frame: CGRectMake(0, self.view.height, self.view.width, adapt_length(length: 400)))
self.contentView?.backgroundColor = .white
self.view.addSubview(self.contentView!)
self.line = UIImageView(frame: CGRect(x: 0, y: adapt_length(length: 49)-DS_RRV_ONE_PIXEL, width: self.view.width, height: DS_RRV_ONE_PIXEL))
self.line?.backgroundColor = HexColor("#EBE9EA")
self.contentView?.addSubview(self.line!)
self.provinceBtn = UIButton(type: .custom)
self.provinceBtn?.frame = CGRect(x: adapt_length(length: 5), y: 0, width: adapt_length(length: 59), height: adapt_length(length: 49))
self.provinceBtn?.setTitle("请选择", for: .normal)
self.provinceBtn?.setTitleColor(HexColor("#333333"), for: .normal)
self.provinceBtn?.clipsToBounds = true
self.provinceBtn?.titleLabel?.font = Font_13
self.provinceBtn?.titleLabel?.lineBreakMode = .byTruncatingTail
self.provinceBtn?.addTarget(self, action: #selector(provinceBtnClick), for: .touchUpInside)
self.contentView?.addSubview(self.provinceBtn!)
self.cityBtn = UIButton(type: .custom)
self.cityBtn?.frame = CGRect(x: self.provinceBtn!.right+adapt_length(length: 5), y: 0, width: adapt_length(length: 59), height: adapt_length(length: 49))
self.cityBtn?.setTitle("城市", for: .normal)
self.cityBtn?.setTitleColor(HexColor("#999999"), for: .normal)
self.cityBtn?.clipsToBounds = true
self.cityBtn?.titleLabel?.font = Font_13
self.cityBtn?.titleLabel?.lineBreakMode = .byTruncatingTail
self.cityBtn?.addTarget(self, action: #selector(cityBtnClick), for: .touchUpInside)
self.contentView?.addSubview(self.cityBtn!)
self.areaBtn = UIButton(type: .custom)
self.areaBtn?.frame = CGRect(x: self.cityBtn!.right+adapt_length(length: 5), y: 0, width: adapt_length(length: 59), height: adapt_length(length: 49))
self.areaBtn?.setTitle("区县", for: .normal)
self.areaBtn?.setTitleColor(HexColor("#999999"), for: .normal)
self.areaBtn?.clipsToBounds = true
self.areaBtn?.titleLabel?.font = Font_13
self.areaBtn?.titleLabel?.lineBreakMode = .byTruncatingTail
self.areaBtn?.addTarget(self, action: #selector(areaBtnClick), for: .touchUpInside)
self.contentView?.addSubview(self.areaBtn!)
self.selectImageView = UIImageView(frame: CGRect(x: 0, y: self.line!.bottom-2, width: adapt_length(length: 39), height: 2))
self.selectImageView?.backgroundColor = HexColor("#1961FE")
self.contentView?.addSubview(self.selectImageView!)
self.selectImageView!.centerX = self.provinceBtn!.centerX
let confirmBtn = UIButton(type: .custom)
confirmBtn.frame = CGRect(x: self.contentView!.width-adapt_length(length: 49), y: 0, width: adapt_length(length: 49), height: adapt_length(length: 49))
confirmBtn.setTitle("确定", for: .normal)
confirmBtn.setTitleColor(HexColor("#333333"), for: .normal)
confirmBtn.clipsToBounds = true
confirmBtn.titleLabel?.font = Font_13
confirmBtn.addTarget(self, action: #selector(confirmBtnClick), for: .touchUpInside)
self.contentView?.addSubview(confirmBtn)
self.contentView?.addSubview(self.tableView)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.displayLevel == 1{
return self.provinceDataArr!.count
}else if self.displayLevel == 2{
return self.cityDataArr!.count
}else if self.displayLevel == 3{
return self.areaDataArr!.count
}else{
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "CNLiveAddrSelectAreaTableViewCellID"
var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
if cell == nil{
cell = UITableViewCell(style: .default, reuseIdentifier: identifier)
cell?.selectionStyle = .none
let titleLab = UILabel(frame: CGRect(x: adapt_length(length: 15), y: 0, width: self.view.width-adapt_length(length: 15)*2, height: adapt_length(length: 40)))
titleLab.font = Font_14
titleLab.textColor = HexColor("#666666")
titleLab.lineBreakMode = .byTruncatingTail
titleLab.tag = 1
cell?.addSubview(titleLab)
}
let titleLab = cell?.viewWithTag(1) as! UILabel
if self.displayLevel == 1{
let model = self.provinceDataArr![indexPath.row]
titleLab.text = model.name
if model.id == self.temp_selectProvinceModel?.id{
titleLab.textColor = HexColor("#1961FE")
}else{
titleLab.textColor = HexColor("#666666")
}
}else if self.displayLevel == 2{
let model = self.cityDataArr![indexPath.row]
titleLab.text = model.name
if model.id == self.temp_selectCityModel?.id{
titleLab.textColor = HexColor("#1961FE")
}else{
titleLab.textColor = HexColor("#666666")
}
}else if self.displayLevel == 3{
let model = self.areaDataArr![indexPath.row]
titleLab.text = model.name
if model.id == self.temp_selectAreaModel?.id{
titleLab.textColor = HexColor("#1961FE")
}else{
titleLab.textColor = HexColor("#666666")
}
}else{
titleLab.text = ""
}
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if self.displayLevel == 1{
let model = self.provinceDataArr![indexPath.row]
self.postDataWithPid(pid: model.id, level: 2){result, status in
self.temp_selectProvinceModel = model
self.temp_selectCityModel = nil
self.temp_selectAreaModel = nil
}
}else if self.displayLevel == 2{
let model = self.cityDataArr![indexPath.row]
self.postDataWithPid(pid: model.id, level: 3){result, status in
self.temp_selectCityModel = model
self.temp_selectAreaModel = nil
}
}else if self.displayLevel == 3{
let model = self.areaDataArr![indexPath.row]
self.temp_selectAreaModel = model
self.tableView.reloadData()
}
}
// MARK: - 触发事件
@objc func shadowViewTap(){
self.hide()
}
@objc func confirmBtnClick(){
if (self.temp_selectProvinceModel == nil) || (self.temp_selectCityModel == nil) || (self.temp_selectAreaModel == nil){
showMessage(message: "省市区未选择完成")
return
}
self.provinceId = self.temp_selectProvinceModel!.id
self.province = self.temp_selectProvinceModel!.name
self.cityId = self.temp_selectCityModel!.id
self.city = self.temp_selectCityModel!.name
self.areaId = self.temp_selectAreaModel!.id
self.area = self.temp_selectAreaModel!.name
self.finishSelectAreaCallBack?()
self.hide()
}
@objc func provinceBtnClick(){
self.displayLevel = 1
self.tableView.reloadData()
self.tableView.setContentOffset(CGPointZero, animated: true)
if self.temp_selectCityModel != nil{
self.cityBtn?.setTitleColor(HexColor("#333333"), for: .normal)
if self.temp_selectAreaModel != nil{
self.areaBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else{
self.areaBtn?.setTitleColor(HexColor("#999999"), for: .normal)
}
}else{
self.cityBtn?.setTitleColor(HexColor("#999999"), for: .normal)
self.areaBtn?.setTitleColor(HexColor("#999999"), for: .normal)
}
}
@objc func cityBtnClick(){
if self.temp_selectProvinceModel == nil{
return
}
self.displayLevel = 2
self.tableView.reloadData()
self.tableView.setContentOffset(CGPointZero, animated: true)
if self.temp_selectAreaModel != nil{
self.areaBtn?.setTitleColor(HexColor("#333333"), for: .normal)
}else{
self.areaBtn?.setTitleColor(HexColor("#999999"), for: .normal)
}
}
@objc func areaBtnClick(){
if self.temp_selectCityModel == nil{
return
}
self.displayLevel = 3
self.tableView.reloadData()
self.tableView.setContentOffset(CGPointZero, animated: true)
}
}