123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- //
- // IHHotelPickerView.swift
- // Inhealth
- //
- // Created by weclouds on 2020/3/11.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- let kNotifactionIHHotelPickerViewGetHistory = "kNotifactionIHHotelPickerViewGetHistory"
- extension DropDownData{
- }
- protocol IHHotelPickerViewDelegate : NSObjectProtocol{
- func selectedHotel(_ indexPath:IndexPath)
- func headerPickHotel()
- func headerPikcerCountry(_ countryId:String)
- func selectedProvince(_ provinceId:String)
- func headerPickerHistroyHotel(_ hotel:DropDownData)
- }
- class IHHotelPickerView: UIView {
- weak var delegate : IHHotelPickerViewDelegate?
- private lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect.zero, style: .grouped)
- tableView.delegate = self
- tableView.dataSource = self
- tableView.backgroundColor = .white
- return tableView
- }()
-
- var isSearch :Bool? = false{
- didSet{
- //self.tableView.reloadData()
- }
- }
- var searchText:String?{
- didSet{
- self.filterContent(for: self.searchText ?? "")
- }
- }
- var fillterResult:[DropDownData]?{
- didSet{
- let(countryArr, titlesArr) = collation.partitionObjects(array: fillterResult! as [AnyObject], collationStringSelector: #selector(getter: DropDownData.name))
- log.debug("countryArr - \(countryArr), titlesArr = \(titlesArr)")
- sectionTitles = titlesArr
- countriesWithSections = countryArr as! [[DropDownData]]
- self.tableView.reloadData()
- }
- }
- var provicelist:[DropDownData]?{
- didSet{
- if let list = self.provicelist {
- if list.count == 0 {
- sectionTitles = ["A"]
- let all = DropDownData()
- all.id = self.countryId //省份列表为空 着 赋值为国家列表
- all.name = "All"
- countriesWithSections = [[all]]
- self.tableView.reloadData()
- }else{
- let(countryArr, titlesArr) = collation.partitionObjects(array: provicelist! as [AnyObject], collationStringSelector: #selector(getter: DropDownData.name))
- log.debug("countryArr - \(countryArr), titlesArr = \(titlesArr)")
- sectionTitles = titlesArr
- countriesWithSections = countryArr as! [[DropDownData]]
- self.tableView.reloadData()
- }
- }
- }
- }
- var countryList:[DropDownData]?{
- didSet{
- headerView?.countries = self.countryList
- }
- }
- private var headerView : IHHotelPickerHeaderView?
-
-
- var hotelName :String?{
- didSet{
- headerView?.currentHotel = self.hotelName
- }
- }
- var countryId:String?
- var countryName:String?{
- didSet{
- headerView?.currentCountry = self.countryName
- }
- }
- let collation = UILocalizedIndexedCollation.current()
- var objects : [String]?
- var countriesWithSections = [[DropDownData]]()
-
- var sectionTitles = [String]()
- var sections :[[String]]?
- override init(frame: CGRect) {
- super.init(frame:frame)
- // self.backgroundColor = UIColor.cyan
- addSubview(tableView)
- //获取历史城市元素
- let hotels = IHHistroyPlist.share.getAllHistoryHotelData()
- let headerHeight = self.calculateHeaderViewHeight(hotels)
- headerView = IHHotelPickerHeaderView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: headerHeight))
- headerView?.delegate = self
- headerView?.historyarr = hotels
- headerView?.backgroundColor = UIColor.white
- tableView.tableHeaderView = headerView
-
- NotificationCenter.default.addObserver(self, selector: #selector(histroyNotify), name: NSNotification.Name(kNotifactionIHHotelPickerViewGetHistory), object: nil)
- }
-
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- deinit {
- NotificationCenter.default.removeObserver(self)
- }
- override func layoutSubviews() {
- super.layoutSubviews()
- tableView.frame = self.bounds
- }
-
- ///过滤
- func filterContent(for searchText:String) {
- let _provicelist = self.provicelist
- let proviceName = searchText
- var fillterProvinceNS : [DropDownData]
-
- if proviceName == "" || _provicelist == nil || _provicelist?.count == 0 { //没有输入则为传入的内容
- fillterProvinceNS = provicelist!
- }else{
- fillterProvinceNS = (_provicelist?.filter({ (province) -> Bool in
- //全部转为小写 在比较
- let name = province.name!.lowercased()
- let search = searchText.lowercased()
- return name.contains(search) == true
- // return province.name!.contains(searchText) == true
- }))!
- }
- fillterResult = fillterProvinceNS
- log.debug(fillterResult)
- }
- @objc func histroyNotify() {
-
- tableView.tableHeaderView = nil
- // let citys = IHHistroyPlist.share.getAllHistoryData()
- let hotels = IHHistroyPlist.share.getAllHistoryHotelData()
- let headerHeight = self.calculateHeaderViewHeight(hotels)
- headerView?.height = headerHeight
- headerView?.historyarr = hotels
- tableView.tableHeaderView = headerView
- // self.tableView.reloadData()
- }
- //计算高度
- func calculateHeaderViewHeight(_ historyArr:[IHHistoryHotelModel]) -> CGFloat {
- var positionY : CGFloat = 5 + 19 + 15
- var positionX : CGFloat = 20
- let lab_h : CGFloat = 30
- let bgView_width : CGFloat = KSCREENWIDTH - 40
- for i in 0..<historyArr.count {
- let city = historyArr[i]
- let str = city.name
- let str_width = str!.ga_widthForComment(font: UIFont(name: PingFangSC_Regular, size: 13)!, height: lab_h)
- let btnWidth = str_width + 20
- positionX += (btnWidth + 10)
-
- if positionX > bgView_width{
- positionX = 20
- positionY += 40
- // lab_h += 30
- // log.debug("label的g高度 -- \(lab_h)")
- }
-
-
- }
- var height : CGFloat
- if historyArr.count == 0 {
-
- height = positionY + 132
-
- }else{
- height = positionY + 132 + 20 + 20
- }
-
- return height
- }
- }
- extension IHHotelPickerView:UITableViewDataSource,UITableViewDelegate{
- func numberOfSections(in tableView: UITableView) -> Int {
- return sectionTitles.count
- }
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return countriesWithSections[section].count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
- if cell == nil {
- cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
- }
- cell?.textLabel?.font = UIFont(name: PingFangSC_Regular, size: 13)
- cell?.textLabel?.textColor = UIColor(hexString: "#333333")
- let province = countriesWithSections[indexPath.section][indexPath.row]
- cell?.textLabel?.text = province.name
- cell?.selectionStyle = .none
- return cell!
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- return 45
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.01
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- let view = UIView()
- let label = UILabel(frame: CGRect(x: 20, y: 0, width: 100, height: 50))
- label.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 16)
- label.textColor = UIColor(hexString: "#333333")
- label.text = sectionTitles[section]
- view.addSubview(label)
- view.backgroundColor = .white
- return view
- }
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return UIView()
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- //选择了
-
- if let delegate = self.delegate {
- let province = countriesWithSections[indexPath.section][indexPath.row]
- var provinceId = province.id
- let provinceName = province.name
- if province.name == "All" {
- provinceId = Intermediate.countryId
- }
- delegate.selectedProvince(provinceId!)
- log.debug("省份ID- \(province.id!)")
- Intermediate.provinceId = province.id!
- Intermediate.provinceName = provinceName!
- // delegate.selectedHotel(indexPath)
- }
-
- }
-
- }
- extension IHHotelPickerView:IHHotelPickerHeaderViewDelegate{
- func pickHistoryHotel(_ hotelId: String, hotelName: String) {
- log.debug("cityid - \(hotelId), cityname- \(hotelName)")
- let hotel = DropDownData()
- hotel.id = hotelId
- hotel.name = hotelName
- if let delegate = self.delegate {
- delegate.headerPickerHistroyHotel(hotel)
- }
- }
-
- func pickViewToSelectCountry(_ countryId: String) {
- log.debug("pickViewToSelectCountry")
- if let delegate = self.delegate {
- delegate.headerPikcerCountry(countryId)
- }
- }
-
- func pickCurrentHotel() {
- log.debug("pickCurrentHotel")
- if let delegate = self.delegate {
- delegate.headerPickHotel()
- }
- }
- }
|