杨锴
2025-05-11 7453d2d0cef415b34323d1b91e6cfa4a6ba31178
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
119
120
121
122
123
124
125
    //
    //  CommonAlertSheetView.swift
    //  WanPai
    //
    //  Created by 无故事王国 on 2023/10/9.
    //
 
import UIKit
import JQTools
 
class CommonAlertSheetView: UIView,JQNibView {
 
    @IBOutlet weak var view_container: UIView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var cons_tableHei: NSLayoutConstraint!
    @IBOutlet weak var cons_viewBottom: NSLayoutConstraint!
 
    private var items = [String]()
    private var clouse:((Int)->Void)!
 
    override func awakeFromNib() {
        super.awakeFromNib()
        alpha = 0
        cons_viewBottom.constant = -JQ_ScreenW
        layoutIfNeeded()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(CommonAlertSheetTCell.self, forCellReuseIdentifier: "TCell")
    }
 
    static func show(items:[String],clouse:@escaping (Int)->Void){
        let alertView = CommonAlertSheetView.jq_loadNibView()
        alertView.frame = sceneDelegate?.window?.frame ?? .zero
        alertView.cons_tableHei.constant = CGFloat(items.count * 50)
        sceneDelegate?.window?.addSubview(alertView)
        alertView.cons_viewBottom.constant = 0
        alertView.items = items
        alertView.clouse = clouse
 
        UIView.animate(withDuration: 0.4) {
            alertView.layoutIfNeeded()
            alertView.alpha = 1
        } completion: { _ in
 
        }
    }
 
    override func layoutSubviews() {
        super.layoutSubviews()
        let totalH = Double(items.count * 50) + UIDevice.jq_safeEdges.bottom + 50
        view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20,height: totalH)
    }
 
    private func hidden(){
        self.cons_viewBottom.constant = -JQ_ScreenW
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
 
    @IBAction func hiddenAction(_ sender: UIButton) {
        hidden()
    }
}
 
extension CommonAlertSheetView:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        clouse(indexPath.row)
        hidden()
    }
}
 
extension CommonAlertSheetView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "TCell") as! CommonAlertSheetTCell
        cell.label_content.text = items[indexPath.row]
        return cell
    }
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
}
 
fileprivate class CommonAlertSheetTCell:UITableViewCell{
 
    var label_content:UILabel!
 
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
 
        selectionStyle = .none
        label_content = UILabel()
        label_content.text = "123"
        label_content.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        label_content.textColor = .black.withAlphaComponent(0.8)
 
        contentView.addSubview(label_content)
        label_content.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
 
        let lineView = UIView()
        lineView.backgroundColor = UIColor(hexStr: "#DBDBE7")
        contentView.addSubview(lineView)
        lineView.snp.makeConstraints { make in
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}