// // Virgil_MenuChooseView.swift // sdjy // // Created by Virgil on 2018/6/15. // Copyright © 2018年 Virgil. All rights reserved. // import UIKit class Virgil_MenuChooseView: UIView, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var btnTemp: UIButton! @IBOutlet weak var tableView: UITableView! /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ private var defaultHeight: CGFloat = 0 private var maxHeight: CGFloat = 0 var arrSelectedIndex = [Int]() var defaultColor: UIColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x323232, alpha: 1.0) var selectedColor: UIColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x15BABC, alpha: 1.0) private var arrTitle = [String]() private var arrMenu = [[Virgil_MenuChooseModel]]() var backFunc: RebackFunction3! private var arrBtns = [UIButton]() ///arrTitle:标题 arrMenus:菜单列表 返回:选中第几列 第几项 选中菜单的名称 func initView(arrTitles: [String], arrMenus: [[Virgil_MenuChooseModel]], funcBack:@escaping RebackFunction3) { backFunc = funcBack for str in arrTitles { arrTitle.append(str) } for temp in arrMenus { arrMenu.append(temp) } let width: CGFloat = self.frame.width / CGFloat(arrTitle.count) var left: CGFloat = 0 for i in 0 ..< arrTitle.count { let btn = UIButton(frame: CGRect(x: left, y: 0, width: width, height: self.frame.height)) btn.setTitle(arrTitle[i], for: .normal) btn.setTitleColor(defaultColor, for: .normal) btn.setTitleColor(selectedColor, for: .selected) btn.setImage(UIImage(named: "back_down"), for: .normal) btn.setImage(UIImage(named: "bavk_up"), for: .selected) left += width btn.setFontSize(fontSize: 14) btn.tag = i self.addSubview(btn) btn.setTitleImageLoction() btn.addTarget(self, action: #selector(btnMenuClick), for: .touchUpInside) btn.isSelected = false arrBtns.append(btn) arrSelectedIndex.append(0) } defaultHeight = self.frame.height tableView.marginTop(top: defaultHeight) tableView.isHidden = true tableView.register(UINib(nibName: "Virgil_MenuChooseTableViewCell", bundle: nil), forCellReuseIdentifier: "Virgil_MenuChooseTableViewCell") tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .none tableView.showsVerticalScrollIndicator = false tableView.delegate = self tableView.dataSource = self btnTemp.marginTop(top: defaultHeight) } var current = 0 @objc func btnMenuClick(btn: UIButton) { current = btn.tag for btnTemp in arrBtns { if btnTemp.tag == btn.tag { btnTemp.isSelected = true } else { btnTemp.isSelected = false } } btnTemp.isHidden = false tableView.isHidden = false tableView.reloadData() maxHeight = self.superview!.frame.height - 80 var dataHeight: CGFloat = CGFloat(self.arrMenu[current].count) * 50 if maxHeight < dataHeight { dataHeight = maxHeight } self.setSizeHeight(height: self.superview!.frame.height) UIView.animate(withDuration: 0.2, animations: { self.btnTemp.alpha = 0.3 self.tableView.setSizeHeight(height: dataHeight) }) { (_) in } } // MARK: TABLEVLEW 实现 // MARK: TABLEVLEW 实现 func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.arrMenu[current].count } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return 50 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Virgil_MenuChooseTableViewCell", for: indexPath as IndexPath) as! Virgil_MenuChooseTableViewCell cell.lblTitle.text = arrMenu[current][indexPath.row].name if arrSelectedIndex[current] == indexPath.row { cell.imgRight.isHidden = false cell.lblTitle.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x15B4B3, alpha: 1.0) } else { cell.lblTitle.textColor = CommonUntils.getUIColorFromRGB(rgbValue: 0x646464, alpha: 1.0) cell.imgRight.isHidden = true } //cell.lblTitle.text = getString(indexPath.row, key: "Name") return cell } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { arrSelectedIndex[current] = indexPath.row hidMenu() backFunc(current, indexPath.row, arrMenu[current][indexPath.row].name) } @IBAction func btnCloseClick(_ sender: Any) { hidMenu() } func hidMenu() { for btnTemp in arrBtns { btnTemp.isSelected = false } UIView.animate(withDuration: 0.2, animations: { self.tableView.setSizeHeight(height: 0) self.btnTemp.alpha = 0 }) { (_) in self.tableView.isHidden = true self.btnTemp.isHidden = true self.setSizeHeight(height: self.defaultHeight) } } }