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