//
|
// CarBrandListView.swift
|
// OKProject
|
//
|
// Created by 无故事王国 on 2022/5/9.
|
// Copyright © 2022 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
|
class CarBrandListView: UIView,LDNibView{
|
|
@IBOutlet weak var tableView: UITableView!
|
@IBOutlet weak var tableViewHeiCons: NSLayoutConstraint!
|
private var hiddenClouse:(()->Void)?
|
private var selectClouse:((Int)->Void)?
|
private var items = [String]()
|
private var selectIndex:Int = 0
|
override func awakeFromNib() {
|
super.awakeFromNib()
|
alpha = 0
|
tableView.delegate = self
|
tableView.dataSource = self
|
tableView.separatorStyle = .none
|
tableViewHeiCons.constant = 0
|
layoutIfNeeded()
|
}
|
|
@discardableResult
|
static func show(_ vc:YYViewController,offsetTop:CGFloat = 0,items:[String],selectIndex:Int = 0,selectClouse:@escaping (Int)->Void,hiddenClouse:@escaping ()->Void)->CarBrandListView{
|
let carBrandListView = CarBrandListView.ld_loadNibView()
|
carBrandListView.selectIndex = selectIndex
|
carBrandListView.hiddenClouse = hiddenClouse
|
carBrandListView.selectClouse = selectClouse
|
carBrandListView.items = items
|
vc.view.addSubview(carBrandListView)
|
carBrandListView.snp.makeConstraints { make in
|
make.edges.equalToSuperview().inset(UIEdgeInsets(top: offsetTop, left: 0, bottom: 0, right: 0))
|
}
|
|
UIView.animate(withDuration: 0.4) {
|
carBrandListView.alpha = 1
|
|
if items.count > 5{
|
carBrandListView.tableViewHeiCons.constant = 44 * 5
|
}else{
|
carBrandListView.tableViewHeiCons.constant = 44 * Double(items.count)
|
}
|
carBrandListView.layoutIfNeeded()
|
}
|
carBrandListView.tableView.reloadData()
|
return carBrandListView
|
}
|
|
|
@IBAction func hiddenAction(_ sender: UIButton) {
|
hidden()
|
}
|
|
func hidden(){
|
UIView.animate(withDuration: 0.4) {
|
self.alpha = 0
|
self.tableViewHeiCons.constant = 0
|
self.layoutIfNeeded()
|
} completion: { _ in
|
self.hiddenClouse?()
|
self.removeFromSuperview()
|
}
|
}
|
|
}
|
|
extension CarBrandListView:UITableViewDelegate{
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
selectIndex = indexPath.row
|
selectClouse?(indexPath.row)
|
hidden()
|
}
|
}
|
|
extension CarBrandListView:UITableViewDataSource{
|
|
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
return 44
|
}
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
return items.count
|
}
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
var cell = tableView.dequeueReusableCell(withIdentifier: "item")
|
if cell == nil{
|
cell = UITableViewCell(style: .default, reuseIdentifier: "item")
|
cell!.selectionStyle = .none
|
cell!.textLabel?.font = UIFont.systemFont(ofSize: 14)
|
|
let lineView = UIView()
|
lineView.backgroundColor = .black.withAlphaComponent(0.1)
|
cell?.contentView.addSubview(lineView)
|
lineView.snp.makeConstraints { make in
|
make.left.right.bottom.equalToSuperview()
|
make.height.equalTo(0.5)
|
}
|
}
|
|
if selectIndex == indexPath.row{
|
cell!.textLabel?.textColor = UIColor(hexString: "#00BF30")
|
}else{
|
cell!.textLabel?.textColor = UIColor(hexString: "#333333")
|
}
|
|
cell!.textLabel?.text = items[indexPath.row]
|
return cell!
|
}
|
|
|
}
|