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