// // IHCurtainCell.swift // Inhealth // // Created by weclouds on 2020/7/25. // Copyright © 2020 weclouds. All rights reserved. // import UIKit let KNotificationCurtain = "KNotificationCurtain" class IHCurtainCell: UITableViewCell { private var labelTitle : UILabel? private var leftBrightnessBtn : UIButton? private var valueBackView : UIView? private var tableView : UITableView? var indexPath : IndexPath? private var choseBtnTag : Int = 0 private var choseValue : String = "" let model = ["关","开"] var dataSource : [String: String]?{ didSet{ if let key = dataSource?.keys.first{ labelTitle?.text = key } if let value = dataSource?.values.first { leftBrightnessBtn?.setTitle(value, for: .normal) } } } class func curtainCellWithTableViewAndIndexPath(tableView : UITableView,indexPath : IndexPath) ->IHCurtainCell { let curtainId = "curtainId" var cell = tableView.dequeueReusableCell(withIdentifier: curtainId) if cell == nil{ cell = IHCurtainCell.init(style: .default, reuseIdentifier: curtainId, tableView: tableView, indexPath: indexPath) } return cell as! IHCurtainCell } init(style: UITableViewCell.CellStyle, reuseIdentifier: String?,tableView : UITableView,indexPath : IndexPath){ super.init(style: style, reuseIdentifier: reuseIdentifier) self.tableView = tableView // self.indexPath = indexPath createUI() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension IHCurtainCell { func createUI(){ let backView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: KSCREENWIDTH, height: 120)) backView.backgroundColor = .white //line let line = UIView.init(frame: CGRect.init(x: 0, y: 119, width: KSCREENWIDTH, height: 1)) line.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0) backView.addSubview(line) //上 let labelTitle = UILabel.init(frame: CGRect.init(x: 5, y: 0, width: KSCREENWIDTH, height: 40)) labelTitle.backgroundColor = UIColor.init(red: 246/255.0, green: 245/255.0, blue: 248/255.0, alpha: 1.0) backView.addSubview(labelTitle) self.labelTitle = labelTitle let h : CGFloat = 50 //下 let choseView = UIView.init(frame: CGRect.init(x: 0, y: 40, width: KSCREENWIDTH, height: h)) backView.addSubview(choseView) let leftBrightnessBtn = UIButton.init(frame: CGRect.init(x: 10, y: 0, width: KSCREENWIDTH - 10 * 2, height: h)) leftBrightnessBtn.setTitle("100%", for: .normal) leftBrightnessBtn.setTitleColor(.black, for: .normal) leftBrightnessBtn.setImage(UIImage.init(named: "xialaIcon"), for: .normal) leftBrightnessBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15.0) leftBrightnessBtn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -20, bottom: 0, right: 0) leftBrightnessBtn.semanticContentAttribute = .forceRightToLeft leftBrightnessBtn.addTarget(self, action: #selector(choseBrightness), for: .touchUpInside) leftBrightnessBtn.tag = 1 //leftView.addSubview(leftBrightnessBtn) choseView.addSubview(leftBrightnessBtn) self.leftBrightnessBtn = leftBrightnessBtn self.contentView.addSubview(backView) } } extension IHCurtainCell { @objc func choseBrightness(sender : UIButton){ self.choseBtnTag = sender.tag if valueBackView != nil { valueBackView?.removeFromSuperview() } createChosePicker(sender: sender) } func createChosePicker(sender : UIButton) { let point = sender.convert(sender.frame.origin, to: self.tableView) var valueBackView = UIView.init(frame: CGRect.init(x: (KSCREENWIDTH - point.x * 2) * 0.4 , y: point.y + 45, width: 90, height: 80)) //屏幕底下的显示 if (KSCREENHEIGHT - point.y) < 80 { //左侧的 valueBackView = UIView.init(frame: CGRect.init(x: (KSCREENWIDTH - point.x * 2) * 0.4, y: point.y - 35 - 80, width: 90, height: 80)) } valueBackView.backgroundColor = .lightGray self.tableView?.addSubview(valueBackView) self.valueBackView = valueBackView let valueTableView = UITableView.init(frame: CGRect.init(x: 1, y: 1, width: 88, height: 78)) valueTableView.dataSource = self valueTableView.delegate = self valueTableView.rowHeight = 40 valueBackView.addSubview(valueTableView) } } extension IHCurtainCell : UITableViewDataSource,UITableViewDelegate{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.model.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let modelId = "modelId" var cell = tableView.dequeueReusableCell(withIdentifier: modelId) if cell == nil { cell = UITableViewCell.init(style: .default, reuseIdentifier: modelId) } cell?.textLabel?.text = self.model[indexPath.row] return cell! } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if self.choseBtnTag == 1 { self.choseValue = model[indexPath.row] leftBrightnessBtn?.setTitle(choseValue, for: .normal) choseBtnTag = 0 valueBackView?.removeFromSuperview() NotificationCenter.default.post(name: NSNotification.Name(rawValue: KNotificationCurtain), object: nil, userInfo: ["indexPath":self.indexPath!,"choseValue":choseValue]) } } }