//
|
// 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
|
}
|
}
|