宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
//  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!
    }
 
 
}