宽窄优行-由【嘉易行】项目成品而来
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
//  BaseTableView.swift
//  Base
//
//  Created by 无故事王国 on 2022/6/15.
//
 
import UIKit
import DZNEmptyDataSet
 
class BaseTableView: UITableView {
 
    enum EmptyType {
        case none
        case empty(image: UIImage?, title: String?)
        case refresh
        case error(errorDescription: String?, recoverySuggestion: String?)
    }
 
    var emptyImage: UIImage?
    var emptyTitle: String?
    var emptyTitleColor: UIColor?
    var emptyTitleFont: UIFont?
    var emptyDescription: String?
    var emptyButonTitle: String?
    var emptyType: EmptyType = .none {
        didSet {
 
            switch emptyType {
 
            case .none:
                emptyImage = nil
                emptyTitle = nil
                emptyDescription = nil
                emptyButonTitle = nil
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            case .empty(let image, let title):
                emptyImage = image
                emptyTitle = title
                emptyDescription = nil
                emptyButonTitle = nil
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            case .refresh:
                emptyImage = #imageLiteral(resourceName: "load")
                emptyTitle = "正在载入..."
                emptyDescription = nil
                emptyButonTitle = nil
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            case .error(let errorDescription, let recoverySuggestion):
                emptyImage = nil
                emptyTitle = errorDescription
                emptyDescription = recoverySuggestion
                emptyButonTitle = "重试"
                emptyTitleColor = #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
                emptyTitleFont = UIFont.systemFont(ofSize: 14)
            }
            reloadEmptyDataSet()
        }
    }
 
    var retryHandler: (() -> Void)?
 
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        setUI()
    }
 
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
 
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
    }
 
 
    private func setUI(){
        separatorStyle = .none
        emptyType = .empty(image: UIImage(named: "icon_empty"), title: "暂无数据")
        emptyDataSetSource = self
        emptyDataSetDelegate = self
        reloadEmptyDataSet()
    }
}
 
extension BaseTableView: DZNEmptyDataSetSource {
 
    func verticalOffset(forEmptyDataSet scrollView: UIScrollView!) -> CGFloat {
        return 0
    }
 
    func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
 
        return emptyImage ?? UIImage()
    }
 
    func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
 
        switch emptyType {
 
        case .refresh:
            return emptyTitle?.makeAttributedString(color: emptyTitleColor!, font: emptyTitleFont!)
 
        default:
            return emptyTitle?.makeAttributedString(color: emptyTitleColor!, font: emptyTitleFont!)
        }
    }
 
    func description(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
 
        return emptyDescription?.makeAttributedString(color: #colorLiteral(red: 0.4, green: 0.4, blue: 0.4, alpha: 1), font: UIFont.systemFont(ofSize: 14))
    }
 
    func buttonTitle(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> NSAttributedString! {
        return emptyButonTitle?.makeAttributedString(color: (state == .normal) ? #colorLiteral(red: 0.6, green: 0.6, blue: 0.6, alpha: 1) : #colorLiteral(red: 0.9215686275, green: 0.9215686275, blue: 0.9215686275, alpha: 1), font: UIFont.systemFont(ofSize: 14))
    }
 
    func buttonBackgroundImage(forEmptyDataSet scrollView: UIScrollView!, for state: UIControl.State) -> UIImage! {
 
        let margin = (UIScreen.main.bounds.width - 32 - 80) / 2
 
        let capInsets = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        let rectInsets = UIEdgeInsets(top: -19.0, left: -margin, bottom: -19.0, right: -margin)
        let image = emptyButonTitle == nil ? nil : ((state == .normal) ? #imageLiteral(resourceName: "button_background_icloud_normal") : #imageLiteral(resourceName: "button_background_icloud_highlight"))
 
        return image?.resizableImage(withCapInsets: capInsets, resizingMode: .stretch).withAlignmentRectInsets(rectInsets)
 
    }
 
    func imageAnimation(forEmptyDataSet scrollView: UIScrollView!) -> CAAnimation! {
 
        let animation = CABasicAnimation(keyPath: "transform")
        animation.fromValue = NSValue(caTransform3D: CATransform3DIdentity)
        animation.toValue = NSValue(caTransform3D: CATransform3DMakeRotation(.pi / 2, 0.0, 0.0, 1.0))
        animation.duration = 0.25
        animation.isCumulative = true
        animation.repeatCount = MAXFLOAT
 
        return animation;
    }
 
}
 
extension BaseTableView: DZNEmptyDataSetDelegate {
 
    func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView!) -> Bool {
        switch emptyType {
 
        case .refresh:
            return true
 
        default:
            return false
        }
    }
 
 
    func emptyDataSet(_ scrollView: UIScrollView!, didTap button: UIButton!) {
        switch emptyType {
            case .error(_, _):
                retryHandler?()
            case .empty(image: _, title: _):
                retryHandler?()
            default:
                break
        }
    }
 
    func emptyDataSetShouldAllowScroll(_ scrollView: UIScrollView!) -> Bool {
        return true
    }
}