// // SBTParamSet_ChargerCell.swift // SolarBT // // Created by weclouds on 2019/3/14. // Copyright © 2019 weclouds. All rights reserved. // import UIKit class SBTextfield: UITextField { } //扩展NSRange,添加转换成Range的方法 extension NSRange { func toRange(string: String) -> Range { let startIndex = string.index(string.startIndex, offsetBy: self.location) let endIndex = string.index(startIndex, offsetBy: self.length) return startIndex..Void)? var isOn :Bool?{ didSet{ if let isOn = self.isOn { if isOn == true { onButton.isSelected = true offButton.isSelected = false }else{ onButton.isSelected = false offButton.isSelected = true } } } } var title : String?{ didSet{ titleLabel.text = title } } var canEdit : Bool? = false{ didSet{ if let canEdit = self.canEdit { onButton.isUserInteractionEnabled = canEdit offButton.isUserInteractionEnabled = canEdit } } } lazy var titleLabel: UILabel = { let titleLabel = UILabel() titleLabel.textColor = UIColor(hexString: "222222") titleLabel.numberOfLines = 0 titleLabel.font = UIFont(name: PingFangSC_Regular, size: 11) return titleLabel }() lazy var onButton : UIButton = { let btn = UIButton() btn.titleLabel?.font = UIFont(name: PingFangSC_Semibold, size: 16) btn.setTitleColor(UIColor(hexString: "#B8B8B8"), for: .normal) btn.setTitleColor(UIColor(hexString: "#222222"), for: .selected) btn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside) btn.setTitle("ON", for: .normal) return btn }() lazy var offButton : UIButton = { let btn = UIButton() btn.titleLabel?.font = UIFont(name: PingFangSC_Semibold, size: 16) btn.setTitleColor(UIColor(hexString: "#B8B8B8"), for: .normal) btn.setTitleColor(UIColor(hexString: "#222222"), for: .selected) btn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside) btn.setTitle("OFF", for: .normal) return btn }() override init(frame: CGRect) { super.init(frame: frame) createUI() self.layer.borderColor = UIColor(hexString: "#D2D2D2")?.cgColor self.layer.borderWidth = 1 self.layer.cornerRadius = 5 self.layer.masksToBounds = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc func buttonClick(_ button: UIButton) { onButton.isSelected = false offButton.isSelected = false button.isSelected = true if self.canEdit == true { if onButton.isSelected == true{ self.switchCallback!(true) } if offButton.isSelected == true{ self.switchCallback!(false) } } } func createUI() { addSubview(titleLabel) addSubview(onButton) addSubview(offButton) titleLabel.snp.makeConstraints { (make) in make.top.equalToSuperview().offset(20) make.left.equalToSuperview().offset(15) make.right.equalToSuperview().offset(-15) } let line = UIView() line.backgroundColor = UIColor(hexString: "#CECECE") addSubview(line) line.snp.makeConstraints { (make) in make.centerX.equalToSuperview() make.bottom.equalToSuperview().offset(-18) make.width.equalTo(1) make.height.equalTo(19) } onButton.snp.makeConstraints { (make) in make.top.equalTo(titleLabel.snp.bottom).offset(8) make.bottom.equalToSuperview().offset(-8) make.left.equalToSuperview() make.right.equalTo(line.snp.left) } offButton.snp.makeConstraints { (make) in make.top.equalTo(titleLabel.snp.bottom).offset(8) make.bottom.equalToSuperview().offset(-8) make.right.equalToSuperview() make.left.equalTo(line.snp.right) } } } class SBTParamSet_balance_Item: UICollectionViewCell { var sb_selected : Bool? = false { didSet{ // if let isselected = self.sb_selected { // if isselected == true{ // //#29B009 // self.layer.borderColor = UIColor(hexString: "#29B009")?.cgColor // }else{ // self.layer.borderColor = UIColor(hexString: "#D2D2D2")?.cgColor // } // } } } var differenceUI : Int? = 0{ didSet{ if let didfference = self.differenceUI { if didfference == 1{ self.contentView.backgroundColor = UIColor(hexString: "#F1F1F1") self.isUserInteractionEnabled = false }else{ self.contentView.backgroundColor = UIColor(hexString: "#FFFFFF") self.isUserInteractionEnabled = true } } } } var canEdit : Bool = false { didSet{ if self.canEdit == true { self.subTitleLabel.textColor = UIColor(hexString: "BBBBBB") // self.subTitleTextfield.isHidden = false // self.subTitleTextfield.placeholder = subTitle // self.subTitleLabel.isHidden = true }else{ self.subTitleLabel.textColor = UIColor(hexString: "222222") // self.subTitleTextfield.isHidden = true // self.subTitleLabel.isHidden = false // self.isTapGuesture = false } } } var balanceTypeCallback:((Int)->Void)? var balanceType : Int? = -1{ didSet{ if self.balanceType != -1 { self.balanceTypeCallback!(self.balanceType!) } } } var title : String?{ didSet{ titleLabel.text = title } } var subTitle : String?{ didSet{ self.subTitleLabel.text = subTitle } } lazy var titleLabel: UILabel = { let titleLabel = UILabel() titleLabel.textColor = UIColor(hexString: "222222") titleLabel.numberOfLines = 0 titleLabel.font = UIFont(name: PingFangSC_Regular, size: 11) return titleLabel }() lazy var subTitleLabel: UILabel = { let subTitleLabel = UILabel() subTitleLabel.textColor = UIColor(hexString: "222222") subTitleLabel.font = UIFont(name: PingFangSC_Semibold, size: 16) subTitleLabel.isHidden = false return subTitleLabel }() override init(frame: CGRect) { super.init(frame: frame) createUI() self.layer.borderColor = UIColor(hexString: "#D2D2D2")?.cgColor self.layer.borderWidth = 1 self.layer.cornerRadius = 5 self.layer.masksToBounds = true } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func createUI() { addSubview(titleLabel) addSubview(subTitleLabel) titleLabel.snp.makeConstraints { (make) in make.top.equalToSuperview().offset(20) make.left.equalToSuperview().offset(15) make.right.equalToSuperview().offset(-15) } subTitleLabel.snp.makeConstraints { (make) in make.left.equalTo(titleLabel.snp.left) make.right.equalTo(titleLabel.snp.right) make.top.equalTo(titleLabel.snp.bottom).offset(8) } self.subTitleLabel.isUserInteractionEnabled = true let tap = UITapGestureRecognizer(target: self, action: #selector(tapAction)) self.subTitleLabel.addGestureRecognizer(tap) } func sb_becameFirstResonder() { tapAction() } @objc func tapAction() { log.debug("手动均衡") let balances = ["Prohibited".da_localizedStr(),"Enable".da_localizedStr()] let balancePick = THScrollChooseView(question: balances, withDefaultDesc:"Prohibited".da_localizedStr()) balancePick!.show() balancePick?.confirmBlock = {[weak self](selectedIndex) in log.debug(balances[selectedIndex]) self?.subTitleLabel.text = balances[selectedIndex] self?.subTitleLabel.textColor = UIColor(hexString: "222222") self?.balanceType = selectedIndex } } } class SBTParamSet_Item: UICollectionViewCell , UITextFieldDelegate{ var beginEditingCallback:(()->Void)? var ChangeCharactersCallback:((String?)->Void)? var sb_selected : Bool? = false { didSet{ // if let isselected = self.sb_selected { // let view = subTitleTextfield.next as! SBTParamSet_Item // if isselected == true{ // // //#29B009 // view.layer.borderColor = UIColor(hexString: "#29B009")?.cgColor // }else{ // view.layer.borderColor = UIColor(hexString: "#D2D2D2")?.cgColor // } // } } } // @objc func didselectedTexfield() { log.debug("测试 UITextField 的点击 ------\(subTitleTextfield.isEditing)") } var differenceUI : Int? = 0{ didSet{ if let didfference = self.differenceUI { if didfference == 1{ self.contentView.backgroundColor = UIColor(hexString: "#F1F1F1") // self.contentView.backgroundColor = UIColor.gray self.isUserInteractionEnabled = false if self.canEdit == true { self.subTitleTextfield.isEnabled = false // self.subTitleTextfield.resignFirstResponder() }else{ self.subTitleTextfield.isEnabled = true } }else{ self.contentView.backgroundColor = UIColor(hexString: "#FFFFFF") self.isUserInteractionEnabled = true } } } } var resultCallback:((String,UITextField)->Void)? var isTapGuesture: Bool = false{ didSet{ } } var itemKeyboardType: UIKeyboardType? = .default{ didSet { self.subTitleTextfield.keyboardType = self.itemKeyboardType! } } var keyboartypeISFloat: Bool? = false{ didSet{ if let keyboartypeISFloat = self.keyboartypeISFloat { if keyboartypeISFloat == true{ // self.subTitleTextfield.keyboardType = .decimalPad }else{ // self.subTitleTextfield.keyboardType = .numberPad } } } } var canEdit : Bool = false { didSet{ if self.canEdit == true { self.subTitleTextfield.isHidden = false self.subTitleTextfield.placeholder = subTitle self.subTitleLabel.isHidden = true }else{ self.subTitleTextfield.isHidden = true self.subTitleLabel.isHidden = false // self.isTapGuesture = false } } } var title : String?{ didSet{ titleLabel.text = title } } var subTitle : String?{ didSet{ self.subTitleLabel.text = subTitle } } lazy var titleLabel: UILabel = { let titleLabel = UILabel() titleLabel.textColor = UIColor(hexString: "222222") titleLabel.numberOfLines = 0 titleLabel.font = UIFont(name: PingFangSC_Regular, size: 11) return titleLabel }() lazy var subTitleLabel: UILabel = { let subTitleLabel = UILabel() subTitleLabel.textColor = UIColor(hexString: "222222") subTitleLabel.font = UIFont(name: PingFangSC_Semibold, size: 16) subTitleLabel.isHidden = false return subTitleLabel }() lazy var subTitleTextfield: UITextField = { let subTitle = UITextField() subTitle.textColor = UIColor(hexString: "222222") // subTitle.backgroundColor = UIColor.blue subTitle.isHidden = true subTitle.addDoneOnKeyboardWithTarget(self, action: #selector(self.doneAction(_:)), shouldShowPlaceholder: true) subTitle.keyboardToolbar.doneBarButton.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor(hexString: "222222") as Any], for: .normal) subTitle.font = UIFont(name: PingFangSC_Semibold, size: 16) return subTitle }() @objc func doneAction(_ sender : UITextField!) { self.endEditing(true) } override init(frame: CGRect) { super.init(frame: frame) createUI() self.layer.borderColor = UIColor(hexString: "#D2D2D2")?.cgColor self.layer.borderWidth = 1 self.layer.cornerRadius = 5 self.layer.masksToBounds = true self.subTitleTextfield.delegate = self } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } //输入完成 func textFieldDidEndEditing(_ textField: UITextField) { if textField.text != ""{ let textFloat = Float(textField.text!) if (textFloat == nil){ makeToast("please_enter_number".da_localizedStr()) textField.text = "" }else{ self.resultCallback!(textField.text!,textField) // if let ChangeCharactersCallback = self.ChangeCharactersCallback { // ChangeCharactersCallback(textField.text) // } } } } func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { // if beginEditingCallback != nil{ // self.beginEditingCallback!() // } // self.sb_selected = true return true } func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { // let currentText = textField.text ?? "" // let newText = currentText.replacingCharacters(in: // range.toRange(string: currentText), with: string) if self.keyboartypeISFloat == true { let toString = (textField.text! as NSString).replacingCharacters(in: range, with: string) let expression = "^[0-9]*((\\\\.|,)[0-9]{0,1})?$"// 正则表达式 保留一位小数 let regex = try! NSRegularExpression(pattern: expression, options: NSRegularExpression.Options.allowCommentsAndWhitespace) let numberOfMatches = regex.numberOfMatches(in: toString, options:NSRegularExpression.MatchingOptions.reportProgress, range: NSMakeRange(0, (toString as NSString).length)) return numberOfMatches != 0 } return true } func createUI() { addSubview(titleLabel) addSubview(subTitleLabel) addSubview(subTitleTextfield) titleLabel.snp.makeConstraints { (make) in make.top.equalToSuperview().offset(20) make.left.equalToSuperview().offset(15) make.right.equalToSuperview().offset(-15) } subTitleLabel.snp.makeConstraints { (make) in make.left.equalTo(titleLabel.snp.left) make.top.equalTo(titleLabel.snp.bottom).offset(8) } subTitleTextfield.snp.makeConstraints { (make) in make.left.equalTo(titleLabel.snp.left) make.top.equalTo(titleLabel.snp.bottom).offset(8) make.right.equalToSuperview().offset(-15) make.bottom.equalToSuperview().offset(-8) } } } class SBTParamSet_ChargerCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource { var isOld: Bool? //放大倍数 var Multiple : Float? // 默认为1倍 var difeffrentUIArr : [Int]? = [Int]() var defaultData : [Double]? = [Double]() var CellBatteryType :BatteryType? = .USER{ didSet{ if self.isOld == true { if let batteryType = self.CellBatteryType { switch batteryType { case .USER: self.difeffrentUIArr = [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1] //self.defaultData = [16.0,15.5,14.4,13.8,13.2,120,-3] case .SLD: self.difeffrentUIArr = [1,1,1,1, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1] case .FLD: self.difeffrentUIArr = [1,1,1,1, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1] case .GEL: self.difeffrentUIArr = [1,1,1,1, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1] case .LI: self.difeffrentUIArr = [1,1,1,1, 1,1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,1,1] } } }else{ if let batteryType = self.CellBatteryType { switch batteryType { case .USER: self.difeffrentUIArr = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] //self.defaultData = [16.0,15.5,14.4,13.8,13.2,120,-3] case .SLD: self.difeffrentUIArr = [0,0,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0] case .FLD: self.difeffrentUIArr = [0,0,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0] case .GEL: self.difeffrentUIArr = [0,0,0,0, 1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,0,0] case .LI: self.difeffrentUIArr = [0,0,0,0, 1,1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,0,0] } } } } } var dataSources:[String]? = [String](){ didSet{ self.collectionView?.reloadData() } } var a :String?{ didSet{ log.debug("====================传过来\(a!)") } } func getData(_ dataSource:[String]?) { } var itemdidSelectedCallBack:((IndexPath)->Void)? var collectionView:UICollectionView? var canEdit: Bool? = false{ didSet{ } } var itemTitles = [String]() // override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { // super.init(style: style, reuseIdentifier: reuseIdentifier) // // dataSources = [String]() // createUI() // } // // required init?(coder aDecoder: NSCoder) { // fatalError("init(coder:) has not been implemented") // } override func awakeFromNib() { super.awakeFromNib() NotificationCenter.default.addObserver(self, selector: #selector(notifionCationSetBatteryType), name: NSNotification.Name(rawValue: kNotifionCationSetBatteryType), object: nil) let def = UserDefaults.standard let istemp = def.value(forKey: TEMPISCENTIGRADE) as? String var Upper_limit_of_charge_temperature = "Upper_limit_of_charge_temperature".da_localizedStr() + "(℃)" var Lower_limit_of_charge_temperature = "Lower_limit_of_charge_temperature".da_localizedStr() + "(℃)" var Upper_limit_of_discharge_temperature = "Upper_limit_of_discharge_temperature".da_localizedStr() + "(℃)" var Lower_limit_of_discharge_temperature = "Lower_limit_of_discharge_temperature".da_localizedStr() + "(℃)" if istemp == "0"{ Upper_limit_of_charge_temperature = "Upper_limit_of_charge_temperature".da_localizedStr() + "(℉)" Lower_limit_of_charge_temperature = "Lower_limit_of_charge_temperature".da_localizedStr() + "(℉)" Upper_limit_of_discharge_temperature = "Upper_limit_of_discharge_temperature".da_localizedStr() + "(℉)" Lower_limit_of_discharge_temperature = "Lower_limit_of_discharge_temperature".da_localizedStr() + "(℉)" }else if istemp == "1"{ Upper_limit_of_charge_temperature = "Upper_limit_of_charge_temperature".da_localizedStr() + "(℃)" Lower_limit_of_charge_temperature = "Lower_limit_of_charge_temperature".da_localizedStr() + "(℃)" Upper_limit_of_discharge_temperature = "Upper_limit_of_discharge_temperature".da_localizedStr() + "(℃)" Lower_limit_of_discharge_temperature = "Lower_limit_of_discharge_temperature".da_localizedStr() + "(℃)" } itemTitles = ["Maximum_charge_current(A)".da_localizedStr(), "Charge_on/off".da_localizedStr(), Upper_limit_of_charge_temperature, Lower_limit_of_charge_temperature, "Overvoltage".da_localizedStr(), "Limited_charge_voltage".da_localizedStr(), "Boost_charge_voltage".da_localizedStr(), "Floating_charge_voltage".da_localizedStr(), "Boost_charge_return_voltage".da_localizedStr(), "Boost_charge_time".da_localizedStr(), "Temperature_compensation_coefficient".da_localizedStr(), "Equalizing_charge_voltage".da_localizedStr(), "Equalizing_charge_time".da_localizedStr(), "Equalizing_charge_interval".da_localizedStr(), "Manual_equalizing".da_localizedStr(), "Over_discharge_return_voltage".da_localizedStr(), "Undervoltage_warning_voltage".da_localizedStr(), "Over_discharge_voltage".da_localizedStr(), "Discharge_cutoff_voltage".da_localizedStr(), "Over_discharge_delay_time".da_localizedStr(), Upper_limit_of_discharge_temperature, Lower_limit_of_discharge_temperature] createUI() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } // 01 10ve01d0005a7cc //设置区别颜色 @objc func notifionCationSetBatteryType() { let batteryType = AppShare.batteryType log.debug("电池类型啊啊啊啊啊啊啊啊啊 -- \(batteryType! )") self.CellBatteryType = batteryType self.collectionView?.reloadData() } func createUI() { let layout = UICollectionViewFlowLayout() let itemWidth = (KSCREENWIDTH - 25 * 3 ) / 2 layout.itemSize = CGSize(width:itemWidth , height: 89) layout.minimumLineSpacing = 12.5 //列间距 layout.minimumInteritemSpacing = 10 //行间距 layout.scrollDirection = .vertical collectionView = UICollectionView(frame: CGRect(x: 25, y: 15, width: KSCREENWIDTH - 50, height: 110 * 10 + 15 ), collectionViewLayout: layout) collectionView?.backgroundColor = UIColor.white collectionView?.showsVerticalScrollIndicator = false collectionView?.showsHorizontalScrollIndicator = false collectionView?.isScrollEnabled = false collectionView?.dataSource = self collectionView?.delegate = self collectionView?.bounces = false for i in 0..<22 { if i != 1 { let identifier = "cell" + "\(i)" collectionView?.register(SBTParamSet_Item.self, forCellWithReuseIdentifier: identifier) } } collectionView?.register(SBTParamSetSwitch_Item.self, forCellWithReuseIdentifier: "cell1") addSubview(collectionView!) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.dataSources!.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { // // log.debug(difeffrentUIArr) if indexPath.row == 1 { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! SBTParamSetSwitch_Item cell.title = itemTitles[indexPath.row] cell.differenceUI = self.difeffrentUIArr![indexPath.row] // log.debug("UI显示不一样-----\(self.difeffrentUIArr![indexPath.row])") let cmd = dataSources![indexPath.row] if cmd == "开" || cmd == "On"{ cell.isOn = true }else if cmd == "关" || cmd == "Off"{ cell.isOn = false } cell.canEdit = canEdit cell.backgroundColor = UIColor.white return cell } else{ let identifier = "cell" + "\(indexPath.row)" let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! SBTParamSet_Item cell.title = itemTitles[indexPath.row] cell.differenceUI = self.difeffrentUIArr![indexPath.row] cell.subTitle = dataSources![indexPath.row] cell.backgroundColor = UIColor.white return cell } } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { itemdidSelectedCallBack!(indexPath) } }