// // IHHotelPickerManager.swift // Inhealth // // Created by weclouds on 2020/3/11. // Copyright © 2020 weclouds. All rights reserved. // import UIKit class IHHotelPickerManager: NSObject ,IHViewManagerProtocolDelegate{ var provincelist:[DropDownData]? /// 懒加载 创建 lazy var hotelPicker: IHCountryPicker = { let picker = IHCountryPicker() picker.modalSize = (width: .full, height: .custom(size: 500)) picker.modalPosition = .bottomCenter picker.cornerRadius = 32 /// 该回调方法可以在本类任意地方写 // picker.selectedAreaCompleted = { [weak self] (province, city, district) in // self?.addressLabel.text = province + " " + city + " " + district // } return picker }() //创建主view容器 lazy var mainView: IHHotelPickerView = { let mainView = IHHotelPickerView() mainView.delegate = self return mainView }() private var searchFiled:UITextField! //私有变量,传递进来的绑定控制器 private weak var vc : UIViewController? override init() { super.init() notificationRegister() } //绑定控制器,主要是 绑定viewdidload这个函数 func bindController(_ vc: UIViewController) { self.vc = vc createUI() requestData() } //布局 func createUI() { mainView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight ) mainView.hotelName = Intermediate.hotelName mainView.countryName = Intermediate.countryName mainView.countryId = Intermediate.countryId self.getProvinceList(Intermediate.countryId) getCountriesList() self.vc?.view.addSubview(mainView) setNavgationBar() } @objc func tapMainView() { searchFiled.resignFirstResponder() } //请求数据 func requestData() { } deinit { NotificationCenter.default.removeObserver(self) } func notificationRegister() { } @objc func itemNotify(_ notify:Notification) { } //获取国家列表 func getCountriesList() { IHAddressService.share.getContryList(requestSuccess: { (countries) in self.mainView.countryList = countries }) { } } //获取省份列表 func getProvinceList(_ countryId:String) { IHAddressService.share.getProvinceList(countryId, requestSuccess: { (provinces) in self.mainView.provicelist = provinces self.provincelist = provinces }) { } } func setNavgationBar() { let searchBtn = UIBarButtonItem(image: UIImage(named: "搜索"), style: .done, target: self, action: #selector(serachAction)) self.vc?.navigationItem.rightBarButtonItem = searchBtn searchFiled = UITextField(frame: CGRect(x: 0, y: 0, width: 300, height: 44)) searchFiled.placeholder = "请输入省份名" searchFiled.font = UIFont(name: Alibaba_PuHuiTi_Regular, size: 14) searchFiled.textColor = UIColor(hexString: "#333333") searchFiled.delegate = self self.vc?.navigationItem.titleView = searchFiled } @objc func serachAction(){ // self.mainView.searchText = searchFiled.text searchFiled.resignFirstResponder()//注销第一响应者 } } extension IHHotelPickerManager:IHHotelPickerViewDelegate{ func headerPickerHistroyHotel(_ hotel: DropDownData) { let picker = IHHistroyHotelPicker() picker.modalSize = (width: .full, height: .custom(size: 300)) picker.modalPosition = .bottomCenter picker.cornerRadius = 32 /// 该回调方法可以在本类任意地方写 // picker.selectedAreaCompleted = { [weak self] (province, city, district) in // self?.addressLabel.text = province + " " + city + " " + district // } picker.hotel = hotel picker.completion = { self.vc?.navigationController?.popViewController(animated: false) } self.vc?.present(picker, animated: true, completion: nil) } func headerPikcerCountry(_ countryId: String) { self.getProvinceList(countryId) } func headerPickHotel() { IHAreaService.share.getBuildNavData(hotelId: Intermediate.hotelId, requestSuccess: { (buildList) in //添加一个all let allBuild = DropDownData() allBuild.id = "" allBuild.name = "所有" var newlist = [DropDownData]() newlist.append(allBuild) for build in buildList{ newlist.append(build) } let picker = IHFloorPicker() let pickerHeight = self.culPickerViewHeight(newlist.count) picker.modalSize = (width: .full, height: .custom(size: pickerHeight)) picker.modalPosition = .bottomCenter picker.cornerRadius = 32 picker.dismissOnTap = false self.vc?.present(picker, animated: true, completion: nil) picker.buildList = newlist picker.hotelName = Intermediate.hotelName picker.completion = {(floor) in self.vc?.navigationController?.popViewController(animated: false) NotificationCenter.default.post(name: NSNotification.Name(kNotificationIHAddressControllerReloadAddress), object: nil) } }) { } } func selectedProvince(_ provinceId: String) { IHAddressService.share.getCityeList(provinceId, requestSuccess: { (cityList) in let picker = IHCountryPicker() picker.modalSize = (width: .full, height: .custom(size: 500)) picker.modalPosition = .bottomCenter picker.cornerRadius = 32 if cityList?.count == 0{ picker.cityId = provinceId } picker.cityList = cityList picker.completion = { self.vc?.navigationController?.popViewController(animated: false) } self.vc?.present(picker, animated: true, completion: nil) }) { } } func selectedHotel(_ indexPath: IndexPath) { } func culPickerViewHeight(_ floorCount: Int) ->Float { if floorCount == 0 { return 76 + 117 }else{ //计算行数 let row : Int = floorCount / 4 let contentViewHight = 30 * (row + 1) + 10 + 10 + 10 * row return 76 + 117 + Float(contentViewHight) } } } extension IHHotelPickerManager:UITextFieldDelegate{ func textFieldDidBeginEditing(_ textField: UITextField) { //开始编辑 } func textFieldDidEndEditing(_ textField: UITextField) { //结束编辑 } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { let currentText = searchFiled.text ?? "" let newText = (currentText as NSString).replacingCharacters(in: range, with: string) self.mainView.searchText = newText log.debug("搜索内容 - \(newText)") if newText == "" { } return true } } extension UILocalizedIndexedCollation{ func partitionObjects(array:[AnyObject], collationStringSelector:Selector) -> ([AnyObject], [String]) { var unsortedSections = [[AnyObject]]() //1. Create a array to hold the data for each section for _ in self.sectionTitles { unsortedSections.append([]) //appending an empty array } //2. Put each objects into a section for item in array { let index:Int = self.section(for: item, collationStringSelector:collationStringSelector) unsortedSections[index].append(item) } //3. sorting the array of each sections var sectionTitles = [String]() var sections = [AnyObject]() for index in 0 ..< unsortedSections.count { if unsortedSections[index].count > 0 { sectionTitles.append(self.sectionTitles[index]) sections.append(self.sortedArray(from: unsortedSections[index], collationStringSelector: collationStringSelector) as AnyObject) } } return (sections, sectionTitles) } }