BaseGroupTableViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // BaseGroupTableViewController.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/6/21.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import EmptyKit
  10. class BaseGroupTableViewController: BaseViewController,Refreshable {
  11. var isEmptyDisplay: Bool = true
  12. var isNotData:Bool = false
  13. var visibleIndexPath: IndexPath?
  14. let tableView: UITableView = UITableView(frame: CGRectZero, style: .grouped).then{
  15. $0.backgroundColor = .db_theme
  16. $0.showsVerticalScrollIndicator = false
  17. $0.tableFooterView = UIView()
  18. }
  19. override func setupConstraints() {
  20. tableView.snp.makeConstraints { (make) in
  21. make.edges.equalTo(0)
  22. }
  23. }
  24. override func viewDidLoad() {
  25. if isNotData {
  26. emptyView.loading_imageview.image = UIImage(named: "animation_loading_error_2")
  27. emptyView.loading_label.text = "暂无数据"
  28. }
  29. super.viewDidLoad()
  30. tableView.ept.dataSource = self
  31. tableView.ept.delegate = self
  32. view.addSubview(tableView)
  33. showLoadAnimation()
  34. registerNetErrorNotification()
  35. }
  36. //MARK: Public Method
  37. //防止异常滚动
  38. func reloadDataWithPreserveScrollPosition() {
  39. // 记录当前可见的 section 和 row
  40. if let indexPaths = tableView.indexPathsForVisibleRows,
  41. let firstIndexPath = indexPaths.first {
  42. visibleIndexPath = firstIndexPath
  43. }
  44. // 调用 reloadData()
  45. tableView.reloadData()
  46. // 手动滚动到之前记录的 section 和 row
  47. if let indexPath = visibleIndexPath {
  48. tableView.scrollToRow(at: indexPath, at: .top, animated: false)
  49. }
  50. }
  51. func startRefresh() {
  52. self.tableView.setContentOffset(.zero, animated: true)
  53. DispatchQueue.delay(time: 0.3, action: {
  54. self.tableView.es.startPullToRefresh()
  55. })
  56. }
  57. func registerNetErrorNotification() {
  58. NotificationCenter.default.rx.notification(custom: .netError)
  59. .subscribe(onNext: {[unowned self] (_) in
  60. self.stopRefresh()
  61. self.showNetErrorView()
  62. })
  63. .disposed(by: disposeBag)
  64. }
  65. func showNetErrorView() {
  66. if totalItems() > 0 { return }
  67. self.showAnimationView(self.tableView, animationType: .failure)
  68. }
  69. func showLoadAnimation() {
  70. self.showAnimationView(self.tableView)
  71. }
  72. func hideLoadAnimation() {
  73. self.hideAnimationView(self.tableView)
  74. }
  75. func stopRefresh() {
  76. guard let isRefreshing = tableView.header?.isRefreshing else { return }
  77. if isRefreshing {
  78. tableView.es.stopPullToRefresh()
  79. }
  80. }
  81. //MARK: Private Method
  82. func stopLoad() {
  83. guard let isLoading = tableView.footer?.isRefreshing else { return }
  84. if isLoading {
  85. tableView.es.stopLoadingMore()
  86. }
  87. }
  88. }
  89. //MARK: Notification
  90. extension BaseGroupTableViewController {
  91. func totalItems() -> Int {
  92. var totalItems: Int = 0
  93. let sectionCount = self.tableView.numberOfSections
  94. for i in 0 ..< sectionCount {
  95. let items = self.tableView.numberOfRows(inSection: i)
  96. totalItems += items
  97. }
  98. return totalItems
  99. }
  100. }
  101. extension BaseGroupTableViewController:EmptyDelegate,EmptyDataSource {
  102. func customViewForEmpty(in view: UIView) -> UIView? {
  103. return EmptyView.loadFromNib()
  104. }
  105. func emptyShouldAllowScroll(in view: UIView) -> Bool {
  106. return true
  107. }
  108. func emptyShouldDisplay(in view: UIView) -> Bool {
  109. return isEmptyDisplay
  110. }
  111. }