younger_times
2023-07-17 a0071c0c7726d3e6742bc7c4c30536302766626a
WanPai/Root/Course/View/CityChooseSubTypeView.swift
@@ -1,29 +1,54 @@
//
//  CityChooseSubTypeView.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/13.
//
    //
    //  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:((String)->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
        tableView.sectionIndexColor = Def_ThemeColor
        tableView.register(UINib(nibName: "CommonSingleTCell", bundle: nil), forCellReuseIdentifier: "_CommonSingleTCell")
        startLocation()
        Services.queryAllCity().subscribe(onNext: {[weak self] data in
            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() ?? []
                self?.tableView.reloadData()
            }
        }).disposed(by: disposeBag)
    }
    @discardableResult
    static func show(inView:UIView,afterView:UIView,clouse:@escaping (String)->Void,closeClouse:@escaping ()->Void)->CityChooseSubTypeView{
    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
@@ -71,3 +96,64 @@
        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, 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 {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_CommonSingleTCell") as! CommonSingleTCell
        if let model = dataDict[keys[indexPath.section]]?[indexPath.row]{
            cell.label_title.text = model.name
        }
        cell.img_select.isHidden = true
        cell.view_container.borderColor = .clear
        cell.view_container.backgroundColor = .white
        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
    }
}