// // IHGatewayListView.swift // Inhealth // // Created by weclouds on 2020/4/7. // Copyright © 2020 weclouds. All rights reserved. // import UIKit protocol IHGatewayListViewDelegate { // func (<#parameters#>) } class IHGatewayListView: UIView { var devDataList:[GatewayData]?{ didSet{ self.tableView.reloadData() } } //编辑的cell var editingIndexPath : IndexPath? lazy var tableView: UITableView = { let tableView = UITableView(frame: .zero, style: .plain) tableView.delegate = self tableView.dataSource = self tableView.allowsSelection = true tableView.separatorStyle = .none tableView.register(UINib(nibName: "IHEquipmentListCell", bundle: nil), forCellReuseIdentifier: "cell") return tableView }() 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() tableView.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH, height: KSCREENHEIGHT - KNavBarHeight ) if self.editingIndexPath != nil { configSwipeButtons() } } } extension IHGatewayListView:UITableViewDelegate,UITableViewDataSource{ func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.devDataList?.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! IHEquipmentListCell cell.selectionStyle = .none cell.gateway = self.devDataList![indexPath.row] return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 135 } //iOS 11 将进入此方法 actions.performsFirstActionWithFullSwipe = NO 可以控制是否自动删除。 func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let deleteAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in completionHandler(true) log.debug("删除") // 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 let delegate = self.delegate{ // // delegate.equipmentHistory(indexPath.row) // let data = self.devDataList![indexPath.row] // delegate.equipmentHistory(data, segmentIndex: self.currentIndex ?? 0) // } } // #05CFAB historyAction.backgroundColor = UIColor(hexString: "#573F95") let settingAction = UIContextualAction(style: .normal, title: "") { (action, sourceView, completionHandler) in completionHandler(true) //处理事件 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, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) { } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { return true } func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) { editingIndexPath = indexPath // log.debug("willBeginEditingRowAt") setNeedsLayout() // 触发layoutSubviews() } //取消选中 func tableView(_ tableView: UITableView, didEndEditingRowAt indexPath: IndexPath?) { editingIndexPath = nil // log.debug("didEndEditingRowAt") } func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool { // log.debug("canMoveRowAt") return true } func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { // log.debug("sourceIndexPath - \(sourceIndexPath) --destinationIndexPath \(destinationIndexPath)") } 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 { configSwipeButton(button, backgroundImage: UIImage(named: "ic_delete_bg")!) }else if i == 1{ configSwipeButton(button, backgroundImage: UIImage(named: "ic_update")!) }else{ configSwipeButton(button, backgroundImage: UIImage(named: "ic_setting_bg")!) } } } 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) } }