IHHotelPickerView.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // IHHotelPickerView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2020/3/11.
  6. // Copyright © 2020 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. let kNotifactionIHHotelPickerViewGetHistory = "kNotifactionIHHotelPickerViewGetHistory"
  10. extension DropDownData{
  11. }
  12. protocol IHHotelPickerViewDelegate : NSObjectProtocol{
  13. func selectedHotel(_ indexPath:IndexPath)
  14. func headerPickHotel()
  15. func headerPikcerCountry(_ countryId:String)
  16. func selectedProvince(_ provinceId:String)
  17. func headerPickerHistroyHotel(_ hotel:DropDownData)
  18. }
  19. class IHHotelPickerView: UIView {
  20. weak var delegate : IHHotelPickerViewDelegate?
  21. private lazy var tableView: UITableView = {
  22. let tableView = UITableView(frame: CGRect.zero, style: .grouped)
  23. tableView.delegate = self
  24. tableView.dataSource = self
  25. tableView.backgroundColor = .white
  26. return tableView
  27. }()
  28. var isSearch :Bool? = false{
  29. didSet{
  30. //self.tableView.reloadData()
  31. }
  32. }
  33. var searchText:String?{
  34. didSet{
  35. self.filterContent(for: self.searchText ?? "")
  36. }
  37. }
  38. var fillterResult:[DropDownData]?{
  39. didSet{
  40. let(countryArr, titlesArr) = collation.partitionObjects(array: fillterResult! as [AnyObject], collationStringSelector: #selector(getter: DropDownData.name))
  41. log.debug("countryArr - \(countryArr), titlesArr = \(titlesArr)")
  42. sectionTitles = titlesArr
  43. countriesWithSections = countryArr as! [[DropDownData]]
  44. self.tableView.reloadData()
  45. }
  46. }
  47. var provicelist:[DropDownData]?{
  48. didSet{
  49. if let list = self.provicelist {
  50. if list.count == 0 {
  51. sectionTitles = ["A"]
  52. let all = DropDownData()
  53. all.id = self.countryId //省份列表为空 着 赋值为国家列表
  54. all.name = "All"
  55. countriesWithSections = [[all]]
  56. self.tableView.reloadData()
  57. }else{
  58. let(countryArr, titlesArr) = collation.partitionObjects(array: provicelist! as [AnyObject], collationStringSelector: #selector(getter: DropDownData.name))
  59. log.debug("countryArr - \(countryArr), titlesArr = \(titlesArr)")
  60. sectionTitles = titlesArr
  61. countriesWithSections = countryArr as! [[DropDownData]]
  62. self.tableView.reloadData()
  63. }
  64. }
  65. }
  66. }
  67. var countryList:[DropDownData]?{
  68. didSet{
  69. headerView?.countries = self.countryList
  70. }
  71. }
  72. private var headerView : IHHotelPickerHeaderView?
  73. var hotelName :String?{
  74. didSet{
  75. headerView?.currentHotel = self.hotelName
  76. }
  77. }
  78. var countryId:String?
  79. var countryName:String?{
  80. didSet{
  81. headerView?.currentCountry = self.countryName
  82. }
  83. }
  84. let collation = UILocalizedIndexedCollation.current()
  85. var objects : [String]?
  86. var countriesWithSections = [[DropDownData]]()
  87. var sectionTitles = [String]()
  88. var sections :[[String]]?
  89. override init(frame: CGRect) {
  90. super.init(frame:frame)
  91. // self.backgroundColor = UIColor.cyan
  92. addSubview(tableView)
  93. //获取历史城市元素
  94. let hotels = IHHistroyPlist.share.getAllHistoryHotelData()
  95. let headerHeight = self.calculateHeaderViewHeight(hotels)
  96. headerView = IHHotelPickerHeaderView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: headerHeight))
  97. headerView?.delegate = self
  98. headerView?.historyarr = hotels
  99. headerView?.backgroundColor = UIColor.white
  100. tableView.tableHeaderView = headerView
  101. NotificationCenter.default.addObserver(self, selector: #selector(histroyNotify), name: NSNotification.Name(kNotifactionIHHotelPickerViewGetHistory), object: nil)
  102. }
  103. required init?(coder: NSCoder) {
  104. fatalError("init(coder:) has not been implemented")
  105. }
  106. deinit {
  107. NotificationCenter.default.removeObserver(self)
  108. }
  109. override func layoutSubviews() {
  110. super.layoutSubviews()
  111. tableView.frame = self.bounds
  112. }
  113. ///过滤
  114. func filterContent(for searchText:String) {
  115. let _provicelist = self.provicelist
  116. let proviceName = searchText
  117. var fillterProvinceNS : [DropDownData]
  118. if proviceName == "" || _provicelist == nil || _provicelist?.count == 0 { //没有输入则为传入的内容
  119. fillterProvinceNS = provicelist!
  120. }else{
  121. fillterProvinceNS = (_provicelist?.filter({ (province) -> Bool in
  122. //全部转为小写 在比较
  123. let name = province.name!.lowercased()
  124. let search = searchText.lowercased()
  125. return name.contains(search) == true
  126. // return province.name!.contains(searchText) == true
  127. }))!
  128. }
  129. fillterResult = fillterProvinceNS
  130. log.debug(fillterResult)
  131. }
  132. @objc func histroyNotify() {
  133. tableView.tableHeaderView = nil
  134. // let citys = IHHistroyPlist.share.getAllHistoryData()
  135. let hotels = IHHistroyPlist.share.getAllHistoryHotelData()
  136. let headerHeight = self.calculateHeaderViewHeight(hotels)
  137. headerView?.height = headerHeight
  138. headerView?.historyarr = hotels
  139. tableView.tableHeaderView = headerView
  140. // self.tableView.reloadData()
  141. }
  142. //计算高度
  143. func calculateHeaderViewHeight(_ historyArr:[IHHistoryHotelModel]) -> CGFloat {
  144. var positionY : CGFloat = 5 + 19 + 15
  145. var positionX : CGFloat = 20
  146. let lab_h : CGFloat = 30
  147. let bgView_width : CGFloat = KSCREENWIDTH - 40
  148. for i in 0..<historyArr.count {
  149. let city = historyArr[i]
  150. let str = city.name
  151. let str_width = str!.ga_widthForComment(font: UIFont(name: PingFangSC_Regular, size: 13)!, height: lab_h)
  152. let btnWidth = str_width + 20
  153. positionX += (btnWidth + 10)
  154. if positionX > bgView_width{
  155. positionX = 20
  156. positionY += 40
  157. // lab_h += 30
  158. // log.debug("label的g高度 -- \(lab_h)")
  159. }
  160. }
  161. var height : CGFloat
  162. if historyArr.count == 0 {
  163. height = positionY + 132
  164. }else{
  165. height = positionY + 132 + 20 + 20
  166. }
  167. return height
  168. }
  169. }
  170. extension IHHotelPickerView:UITableViewDataSource,UITableViewDelegate{
  171. func numberOfSections(in tableView: UITableView) -> Int {
  172. return sectionTitles.count
  173. }
  174. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  175. return countriesWithSections[section].count
  176. }
  177. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  178. var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
  179. if cell == nil {
  180. cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
  181. }
  182. cell?.textLabel?.font = UIFont(name: PingFangSC_Regular, size: 13)
  183. cell?.textLabel?.textColor = UIColor(hexString: "#333333")
  184. let province = countriesWithSections[indexPath.section][indexPath.row]
  185. cell?.textLabel?.text = province.name
  186. cell?.selectionStyle = .none
  187. return cell!
  188. }
  189. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  190. return 45
  191. }
  192. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  193. return 0.01
  194. }
  195. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  196. let view = UIView()
  197. let label = UILabel(frame: CGRect(x: 20, y: 0, width: 100, height: 50))
  198. label.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 16)
  199. label.textColor = UIColor(hexString: "#333333")
  200. label.text = sectionTitles[section]
  201. view.addSubview(label)
  202. view.backgroundColor = .white
  203. return view
  204. }
  205. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  206. return UIView()
  207. }
  208. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  209. //选择了
  210. if let delegate = self.delegate {
  211. let province = countriesWithSections[indexPath.section][indexPath.row]
  212. var provinceId = province.id
  213. let provinceName = province.name
  214. if province.name == "All" {
  215. provinceId = Intermediate.countryId
  216. }
  217. delegate.selectedProvince(provinceId!)
  218. log.debug("省份ID- \(province.id!)")
  219. Intermediate.provinceId = province.id!
  220. Intermediate.provinceName = provinceName!
  221. // delegate.selectedHotel(indexPath)
  222. }
  223. }
  224. }
  225. extension IHHotelPickerView:IHHotelPickerHeaderViewDelegate{
  226. func pickHistoryHotel(_ hotelId: String, hotelName: String) {
  227. log.debug("cityid - \(hotelId), cityname- \(hotelName)")
  228. let hotel = DropDownData()
  229. hotel.id = hotelId
  230. hotel.name = hotelName
  231. if let delegate = self.delegate {
  232. delegate.headerPickerHistroyHotel(hotel)
  233. }
  234. }
  235. func pickViewToSelectCountry(_ countryId: String) {
  236. log.debug("pickViewToSelectCountry")
  237. if let delegate = self.delegate {
  238. delegate.headerPikcerCountry(countryId)
  239. }
  240. }
  241. func pickCurrentHotel() {
  242. log.debug("pickCurrentHotel")
  243. if let delegate = self.delegate {
  244. delegate.headerPickHotel()
  245. }
  246. }
  247. }