无故事王国
2024-03-13 919d1d6bb0ec4043ec25b58a492618239ca37529
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
//
//  CourseSubTypeView.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/9.
//
 
import UIKit
import JQTools
 
class CourseSubTypeView: UIView,JQNibView{
 
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var cons_tableHeight: NSLayoutConstraint!
    private var closeClouse:(()->Void)?
    private var clouse:((NormalSimpleModel)->Void)?
    private var items = [NormalSimpleModel]()
    private var selectModel:NormalSimpleModel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "Common_1_TCell", bundle: nil), forCellReuseIdentifier: "_Common_1_TCell")
        alpha = 0
        cons_tableHeight.constant = 0
        layoutIfNeeded()
    }
 
    @discardableResult
    static func show(inView:UIView,afterView:UIView,items:[NormalSimpleModel],selectModel:NormalSimpleModel? = nil,clouse:@escaping (NormalSimpleModel)->Void,closeClouse:@escaping ()->Void)->CourseSubTypeView{
        let subTypeView = CourseSubTypeView.jq_loadNibView()
        subTypeView.closeClouse = closeClouse
        subTypeView.clouse = clouse
        subTypeView.items = items
        subTypeView.selectModel = selectModel
        inView.addSubview(subTypeView)
        subTypeView.snp.makeConstraints { make in
            make.top.equalTo(afterView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        UIView.animate(withDuration: 0.2) {
            subTypeView.alpha = 1
        } completion: { _ in
            subTypeView.cons_tableHeight.constant = CGFloat(min(items.count * 50, 250))
            UIView.animate(withDuration: 0.2) {
                subTypeView.layoutIfNeeded()
                subTypeView.tableView.reloadData()
            }
        }
 
        return subTypeView
    }
    
    @IBAction func closeAction(_ sender: UIButton) {
        cons_tableHeight.constant = 0
        UIView.animate(withDuration: 0.2) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.closeClouse?()
        }
    }
}
 
extension CourseSubTypeView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        cons_tableHeight.constant = 0
        UIView.animate(withDuration: 0.2) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            let item = self.items[indexPath.row]
            self.clouse?(item)
            self.closeClouse?()
        }
    }
}
 
extension CourseSubTypeView:UITableViewDataSource{
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_Common_1_TCell") as! Common_1_TCell
        let item = items[indexPath.row]
        cell.label_content.text = item.name
        if selectModel == nil{
            cell.isSelected = false
        }else{
            cell.isSelected = selectModel?.name == item.name
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    
}