123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- //
- // IHGatewayView.swift
- // Inhealth
- //
- // Created by weclouds on 2020/8/20.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- protocol IHGatewayViewDelete : NSObjectProtocol{
- func gatewaySelected(gateway : gatewayInfor)
-
- }
- class IHGatewayView: UIView {
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: self.frame, style: .plain)
- tableView.delegate = self
- tableView.dataSource = self
- tableView.allowsSelection = true
-
- tableView.separatorStyle = .none
- tableView.register(UINib(nibName: "IHGatewayCell", bundle: nil), forCellReuseIdentifier: "gatewayCell")
- return tableView
- }()
-
- var gatewayList : [gatewayInfor]?
- //编辑的cell
- var editingIndexPath : IndexPath?
- weak var delete : IHGatewayViewDelete?
- override init(frame: CGRect) {
- super.init(frame: frame)
- addSubview(tableView)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func layoutSubviews() {
- super.layoutSubviews()
- if self.editingIndexPath != nil {
- configSwipeButtons()
- }
- }
- }
-
- extension IHGatewayView : UITableViewDataSource,UITableViewDelegate{
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return gatewayList?.count ?? 0
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "gatewayCell") as! IHGatewayCell
- cell.backgroundColor = .white
- cell.selectionStyle = .none
- let gatawayInfor = gatewayList![indexPath.row]
- cell.gatawayInfor = gatawayInfor
- return cell
- }
-
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 136
- }
- func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
-
- log.debug("左滑")
- #warning("快速移动的时候,如不加此方法,就有可能不执行layoutsubviews")
- self.tableView(self.tableView, willBeginEditingRowAt: indexPath)
-
- let deleteAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
- completionHandler(true)
- log.debug("删除")
-
-
- // if self.currentIndex == 0{
- // if let devDataList = self.devDataList {
- // if indexPath.row < devDataList.count {
- // if let delegate = self.delegate{
- // let devData = self.devDataList![indexPath.row]
- // delegate.equipmentListDeleteDevice(devData.devType!, devId: devData.id!, segmentIndex: self.currentIndex ?? 0)
- // }
- // }else{
- // if let delegate = self.delegate {
- // // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 5)
- // let gateway = self.gatewayList![indexPath.row - devDataList.count ]
- // delegate.gatewayDelegate(gateway, segmentIndex: self.currentIndex ?? 0)
- // }
- // }
- // }
- // }else if self.currentIndex == 5{
- // if let delegate = self.delegate {
- // let gateway = self.gatewayList![indexPath.row ]
- // delegate.gatewayDelegate(gateway, segmentIndex: self.currentIndex ?? 5)
- // }
- // }else{
- // if let delegate = self.delegate{
- // let devData = self.devDataList![indexPath.row]
- // delegate.equipmentListDeleteDevice(devData.devType!, devId: devData.id!, segmentIndex: self.currentIndex ?? 0)
- // }
- // }
- //
- //处理事件
- }
- deleteAction.backgroundColor = UIColor(hexString: "#EBEFF2")
-
- let historyAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
- completionHandler(true)
- //处理事件0
- log.debug("历史数据")
- // if self.currentIndex == 0{
- // if let devDataList = self.devDataList {
- // if indexPath.row < devDataList.count {
- // if let delegate = self.delegate{
- // let data = self.devDataList![indexPath.row]
- // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0)
- // }
- // }else{
- // if let delegate = self.delegate {
- // let data = self.gatewayList![indexPath.row - devDataList.count ]
- // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 0)
- // }
- // }
- // }
- // }else if self.currentIndex == 5{
- // if let delegate = self.delegate {
- // let data = self.gatewayList![indexPath.row]
- // delegate.gatewayUpdate(segmentIndex: self.currentIndex ?? 5)
- // }
- // }else{
- // if let delegate = self.delegate{
- // let data = self.devDataList![indexPath.row]
- // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0)
- // }
- // }
- //
- }
- //#31D4C1 #0A82CF
- historyAction.backgroundColor = UIColor(hexString: "#31D4C1")
- let settingAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in
- completionHandler(true)
- // if self.currentIndex == 0{
- // if let devDataList = self.devDataList {
- // if indexPath.row < devDataList.count {
- // if let delegate = self.delegate{
- // let data = self.devDataList![indexPath.row]
- // delegate.equipmentSetting(data, segmentIndex: self.currentIndex ?? 0)
- // }
- // }else{
- // if let delegate = self.delegate{
- // let data = self.gatewayList![indexPath.row - devDataList.count ]
- // delegate.gatewaySetting(data, segmentIndex: self.currentIndex ?? 5)
- // }
- // }
- // }
- // }else if self.currentIndex == 5{
- // if let delegate = self.delegate {
- // let data = self.gatewayList![indexPath.row ]
- // delegate.gatewaySetting(data, segmentIndex: self.currentIndex ?? 5)
- // }
- // }else{
- // log.debug("设置")
- // if let delegate = self.delegate{
- // let data = self.devDataList![indexPath.row]
- // delegate.equipmentSetting(data, segmentIndex: self.currentIndex ?? 0)
- // }
- //
- // }
- // //处理事件
-
- }
- //#05CFAB
- settingAction.backgroundColor = UIColor(hexString: "#573F95")
-
- let actions = UISwipeActionsConfiguration(actions: [settingAction,historyAction,deleteAction])
- actions.performsFirstActionWithFullSwipe = false
- return actions
-
- }
-
- func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- return false
- //return true
- }
- func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
- editingIndexPath = indexPath
- setNeedsLayout() // 触发layoutSubviews()
-
- }
- //取消选中
- func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) {
- editingIndexPath = nil
- // log.debug("didEndEditingRowAt")
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
- let gatawayInfor = gatewayList![indexPath.row]
- self.delete?.gatewaySelected(gateway : gatawayInfor)
- }
-
-
- }
- extension IHGatewayView{
- func configSwipeButtons() {
- // log.debug("重新绘制 ")
- //iOS 11 层级 UITableView -> UISwipActionPull
- var swipeButtons : [UIView]?
- if #available(iOS 11, *) {
- swipeButtons = self.tableView.getSwipeButtonView()
- }else{
- // iOS 8-10: 画UITableView->UITableViewCell->UITableVIewCellDeleteConfirmationView
- let tabCell = self.tableView.cellForRow(at: self.editingIndexPath!)
- swipeButtons = tabCell?.getSwipeButtonView()
- }
- // log.debug("子控件 -- \(swipeButtons)")
- if swipeButtons?.count == 0 || swipeButtons == nil {
- return
- }
- for i in 0..<(swipeButtons?.count)!{
- let button = swipeButtons![i] as! UIButton
- if i == 0 {
- //ic_delete_bg
- configSwipeButton(button, backgroundImage: UIImage(named: "删除")!)
- }else if i == 1{
- configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!)
-
- }else{
- //ic_setting_bg
- configSwipeButton(button, backgroundImage: UIImage(named: "设置-紫")!)
- }
- }
- }
-
- func configSwipeButton(_ button: UIButton,backgroundImage:UIImage) {
- button.layer.cornerRadius = 5
- button.layer.masksToBounds = true
- button.setBackgroundImage(backgroundImage, for: .normal)
- button.setBackgroundImage(backgroundImage, for: .selected)
- button.titleLabel?.font = UIFont(name: PingFangSC_Medium, size: 11)
-
- let frame = button.frame
- button.frame = CGRect(x: frame.origin.x + 5 , y: frame.origin.y + 10, width: frame.size.width - 10, height: frame.size.height - 20)
- log.debug("frame\(button.frame)")
- }
- }
|