BaseTableViewController.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // BaseTableViewController.swift
  3. // JSJP_Student_sw
  4. //
  5. // Created by Ning.ge on 2023/6/1.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import EmptyKit
  10. class BaseTableViewController: BaseViewController,Refreshable {
  11. var isEmptyDisplay: Bool = true
  12. var isNotData:Bool = false
  13. let tableView: UITableView = UITableView().then{
  14. $0.backgroundColor = .db_theme
  15. $0.showsVerticalScrollIndicator = false
  16. $0.tableFooterView = UIView()
  17. }
  18. override func setupConstraints() {
  19. tableView.snp.makeConstraints { (make) in
  20. make.edges.equalTo(0)
  21. }
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. if isNotData {
  26. emptyView.loading_imageview.image = UIImage(named: "animation_loading_error_2")
  27. emptyView.loading_label.text = "暂无数据"
  28. emptyView.loading_layout_h.constant = 120
  29. }
  30. tableView.ept.dataSource = self
  31. tableView.ept.delegate = self
  32. view.addSubview(tableView)
  33. showLoadAnimation()
  34. registerNetErrorNotification()
  35. }
  36. //MARK: Public Method
  37. func startRefresh() {
  38. self.tableView.setContentOffset(.zero, animated: true)
  39. DispatchQueue.delay(time: 0.3, action: {
  40. self.tableView.es.startPullToRefresh()
  41. })
  42. }
  43. func registerNetErrorNotification() {
  44. NotificationCenter.default.rx.notification(custom: .netError)
  45. .subscribe(onNext: {[unowned self] (_) in
  46. self.stopRefresh()
  47. self.showNetErrorView()
  48. })
  49. .disposed(by: disposeBag)
  50. }
  51. func showNetErrorView() {
  52. if totalItems() > 0 { return }
  53. self.showAnimationView(self.tableView, animationType: .failure)
  54. }
  55. func showLoadAnimation() {
  56. self.showAnimationView(self.tableView)
  57. }
  58. func hideLoadAnimation() {
  59. self.hideAnimationView(self.tableView)
  60. }
  61. func stopRefresh() {
  62. guard let isRefreshing = tableView.header?.isRefreshing else { return }
  63. if isRefreshing {
  64. tableView.es.stopPullToRefresh()
  65. }
  66. }
  67. //MARK: Private Method
  68. func stopLoad() {
  69. guard let isLoading = tableView.footer?.isRefreshing else { return }
  70. if isLoading {
  71. tableView.es.stopLoadingMore()
  72. }
  73. }
  74. }
  75. //MARK: Notification
  76. extension BaseTableViewController {
  77. func totalItems() -> Int {
  78. var totalItems: Int = 0
  79. let sectionCount = self.tableView.numberOfSections
  80. for i in 0 ..< sectionCount {
  81. let items = self.tableView.numberOfRows(inSection: i)
  82. totalItems += items
  83. }
  84. return totalItems
  85. }
  86. }
  87. extension BaseTableViewController:EmptyDelegate,EmptyDataSource {
  88. func customViewForEmpty(in view: UIView) -> UIView? {
  89. return emptyView
  90. }
  91. func emptyShouldAllowScroll(in view: UIView) -> Bool {
  92. return true
  93. }
  94. func emptyShouldDisplay(in view: UIView) -> Bool {
  95. return isEmptyDisplay
  96. }
  97. }
  98. extension BaseTableViewController:UIScrollViewDelegate {
  99. func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  100. self.view.endEditing(true)
  101. }
  102. }