无故事王国
2023-10-11 f7e33a3255d9f87b20e4a06fc32012eaad77cad5
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
//
//  CouponChooseView.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/12.
//
 
import UIKit
import JQTools
 
class CouponChooseView: UIView,JQNibView{
 
    @IBOutlet weak var view_container: UIView!
    @IBOutlet weak var cons_bottom: NSLayoutConstraint!
    @IBOutlet weak var tableView: UITableView!
 
    private var models = [CouponInfoModel]()
    private var clouse:((CouponInfoModel?)->Void)!
    private var defaultModel:CouponInfoModel?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        cons_bottom.constant = -(JQ_ScreenW * 1.1)
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(UINib(nibName: "CouponTCell", bundle: nil), forCellReuseIdentifier: "_CouponTCell")
        alpha = 0
        layoutIfNeeded()
    }
    
    static func show(_ models:[CouponInfoModel],defaultModel:CouponInfoModel? = nil,clouse:@escaping (CouponInfoModel?)->Void){
        let couponView = CouponChooseView.jq_loadNibView()
        couponView.models = models
        couponView.defaultModel = defaultModel
 
        couponView.frame = sceneDelegate?.window?.frame ?? .zero
        sceneDelegate?.window?.addSubview(couponView)
        couponView.cons_bottom.constant = 0
        couponView.clouse = clouse
        
        UIView.animate(withDuration: 0.4) {
            couponView.alpha = 1
            couponView.layoutIfNeeded()
        }completion: { status in
            couponView.tableView.reloadData()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        view_container.jq_addCorners(corner: [.topLeft,.topRight], radius: 20)
    }
    
    @IBAction func cancelAction(_ sender: UIButton) {
        self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
    
    @IBAction func completeAction(_ sender: UIButton) {
        clouse(defaultModel)
        self.cons_bottom.constant = -(JQ_ScreenW * 1.1)
        UIView.animate(withDuration: 0.4) {
            self.alpha = 0
            self.layoutIfNeeded()
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
}
 
extension CouponChooseView:UITableViewDelegate{
 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = models[indexPath.row]
        if defaultModel?.id == model.id{
            defaultModel = nil
        }else{
            defaultModel = model
        }
        tableView.reloadData()
    }
 
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 115
    }
}
 
extension CouponChooseView:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let model = models[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "_CouponTCell") as! CouponTCell
        cell.couponInfoModel = model
        cell.btn_more.isHidden = true
        cell.img_select.image = defaultModel?.id == model.id ? UIImage(named: "btn_choose_s") : UIImage(named: "btn_choose_1")
        return cell
    }
}