Robert
1 天以前 c547797c9267e2f3e3c24c7acb31502517f3b6e6
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
    //
    //  WelfareCouponsSubListVC.swift
    //  WanPai
    //
    //  Created by 无故事王国 on 2023/6/28.
    //
 
import UIKit
import RxSwift
import RxRelay
import JQTools
 
let WelfareCoupons_Noti = NSNotification.Name("WelfareCoupons_Noti")
 
class WelfareCouponViewModel:RefreshModel<CouponModel>{
    var type = BehaviorRelay<Int?>(value: nil)
    var status = BehaviorRelay<Int?>(value: nil)
    override func api() -> (Observable<BaseResponse<[CouponModel]>>)? {
        Services.myCouponList(type: type.value, useStatus: status.value)
    }
}
 
class WelfareCouponsSubListVC: BaseVC {
 
    lazy private var tableView:BaseTableView = {
        let table = BaseTableView(frame: .zero, style: .plain)
        table.separatorStyle = .none
        table.delegate = self
        table.dataSource = self
        table.register(UINib(nibName: "WelfareCouponsTCell", bundle: nil), forCellReuseIdentifier: "_WelfareCouponsTCell")
        return table
    }()
 
    let viewModel = WelfareCouponViewModel()
    private var rows = Set<Int>()
 
 
    required init(type:Int?) {
        super.init(nibName: nil, bundle: nil)
        viewModel.type.accept(type)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel.configure(tableView,needMore: false)
        viewModel.beginRefresh()
    }
 
    override func setUI() {
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
//        tableView.jq_setEmptyView()
    }
 
    override func setRx() {
        NotificationCenter.default.rx.notification(WelfareCoupons_Noti).take(until: self.rx.deallocated).subscribe(onNext: {noti in
            if let row = noti.object as? Int{
                if self.rows.contains(row){
                    self.rows.remove(row)
                }else{
                    self.rows.insert(row)
                }
                self.tableView.beginUpdates()
                self.tableView.reloadRows(at: [IndexPath(row: row, section: 0)], with: .fade)
                self.tableView.endUpdates()
            }
        }).disposed(by: disposeBag)
    }
}
 
extension WelfareCouponsSubListVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let model = viewModel.dataSource.value[indexPath.row]
        if rows.contains(where: {$0 == indexPath.row}){
            let h = max(String.jq_getHeight(text: model.instructionsForUse, width: JQ_ScreenW - 200, font: 12), 30)
            return h + 125
        }else{
            return 125
        }
    }
}
 
extension WelfareCouponsSubListVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.dataSource.value.count
    }
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_WelfareCouponsTCell", for: indexPath) as! WelfareCouponsTCell
//        let cell = tableView.dequeueReusableCell(withIdentifier: "_WelfareCouponsTCell") as! WelfareCouponsTCell
        cell.indexPath = indexPath
        cell.btn_info.isSelected = rows.contains(indexPath.row)
        cell.label_info.isHidden = !rows.contains(indexPath.row)
        cell.view_line.isHidden = !rows.contains(indexPath.row)
        cell.couponModel = viewModel.dataSource.value[indexPath.row]
        return cell
    }
 
}