宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  DistanceView.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/6/13.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import HandyJSON
struct CityModel:HandyJSON{
    var code = 0
    var name = ""
    var citylist:[CitySubModel]?
}
 
struct CitySubModel:HandyJSON{
    var code = 0
    var name = ""
    var arealist:[CistySub1Mode]?
}
 
struct CistySub1Mode:HandyJSON{
    var code = 0
    var name = ""
}
 
class DistanceView: UIView,LDNibView{
 
    @IBOutlet weak var provinceTableView: UITableView!
    @IBOutlet weak var cityTableView: UITableView!
    private var citys = Array<CityModel?>()
    private var selectIndex = 0
    var completeClouse:((Int,String,Int)->Void)?
    var cancelClouse:(()->Void)?
 
    override func awakeFromNib() {
        super.awakeFromNib()
        alpha = 0
        provinceTableView.separatorStyle = .none
        provinceTableView.delegate = self
        provinceTableView.dataSource = self
        cityTableView.separatorStyle = .none
        cityTableView.delegate = self
        cityTableView.dataSource = self
 
        let bundle = Bundle(for: self.classForCoder)
        if let url = bundle.url(forResource: "BRPickerView", withExtension: "bundle"){
            if let plistBundle  = Bundle(url: url){
                let filePath = plistBundle.path(forResource: "BRCity", ofType:"plist")
                if let array = NSArray(contentsOf: URL(fileURLWithPath: filePath!)){
                    var temp = JSONDeserializer<CityModel>.deserializeModelArrayFrom(array: array)!
                    var allModel = CityModel()
                    allModel.code = -1
                    allModel.name = "全国"
                    let sub = CitySubModel(code: -1, name: "不限", arealist: nil)
                    allModel.citylist = [sub]
                    temp.insert(allModel, at: 0)
                    self.citys = temp
                    provinceTableView.reloadData()
                    cityTableView.reloadData()
                }
            }
        }
    }
 
    @discardableResult
    static func show(_ vc:YYViewController,offsetTop:CGFloat = 0,selectIndex:Int)->DistanceView{
        let carBrandListView = DistanceView.ld_loadNibView()
        vc.view.addSubview(carBrandListView)
        carBrandListView.snp.makeConstraints { make in
            make.edges.equalToSuperview().inset(UIEdgeInsets(top: offsetTop, left: 0, bottom: 0, right: 0))
        }
 
        carBrandListView.selectIndex = selectIndex
 
        UIView.animate(withDuration: 0.4) {
            carBrandListView.alpha = 1
            carBrandListView.layoutIfNeeded()
        }
 
        return carBrandListView
    }
 
    @IBAction func hiddenAction(_ sender: UIButton) {
        hidden()
    }
 
    func hidden(){
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
            self.cancelClouse?()
        }
    }
}
 
extension DistanceView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView == provinceTableView{
            selectIndex = indexPath.row
        }
 
        if tableView == cityTableView{
            if citys[selectIndex]?.citylist?[indexPath.row].name.isEmpty ?? true{
                let code = citys[selectIndex]!.code
                let name = citys[selectIndex]!.name
                completeClouse?(code,name,selectIndex)
            }else{
                let code = citys[selectIndex]!.citylist![indexPath.row].code
                let name = citys[selectIndex]!.citylist![indexPath.row].name
                completeClouse?(code,name,selectIndex)
            }
            hidden()
        }
 
        cityTableView.reloadData()
        tableView.reloadData()
    }
}
 
extension DistanceView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if  tableView == provinceTableView{
            return citys.count
        }
 
        if tableView == cityTableView{
            return citys[selectIndex]?.citylist?.count ?? 0
        }
 
        return 0
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil{
            cell = UITableViewCell(style: .default, reuseIdentifier:"cell")
            cell?.selectionStyle = .none
        }
 
        if #available(iOS 14.0, *) {
            var content = cell?.defaultContentConfiguration()
            content?.textProperties.alignment = .center
            content?.textProperties.font = UIFont.systemFont(ofSize: 15)
            if  tableView == provinceTableView{
                content?.text = citys[indexPath.row]?.name
                if selectIndex == indexPath.row{
                    content?.textProperties.color = UIColor(hexString: "#00BF30")!
                }else{
                    content?.textProperties.color = .black
                }
            }
 
            if tableView == cityTableView{
                if citys[selectIndex]?.citylist?[indexPath.row].name.isEmpty ?? true{
                    content?.text = "不限"
                }else{
                    content?.text = citys[selectIndex]?.citylist?[indexPath.row].name
                }
                cell?.textLabel?.textColor = .black
            }
 
            cell?.contentConfiguration = content
        } else {
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            cell?.textLabel?.textAlignment = .center
 
            if  tableView == provinceTableView{
                if selectIndex == indexPath.row{
                    cell?.textLabel?.textColor = UIColor(hexString: "#00BF30")!
                }else{
                    cell?.textLabel?.textColor = .black
                }
                cell?.textLabel?.text = citys[indexPath.row]?.name
            }
 
            if tableView == cityTableView{
                if citys[selectIndex]?.citylist?[indexPath.row].name.isEmpty ?? true{
                    cell?.textLabel?.text = "不限"
                }else{
                    cell?.textLabel?.text = citys[selectIndex]?.citylist?[indexPath.row].name
                }
                cell?.textLabel?.textColor = .black
            }
        }
        return cell!
    }
 
 
}