BaseGroupTableViewController.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 visibleIndexPath: IndexPath?
  13. let tableView: UITableView = UITableView(frame: CGRectZero, style: .grouped).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. tableView.ept.dataSource = self
  26. tableView.ept.delegate = self
  27. view.addSubview(tableView)
  28. showLoadAnimation()
  29. registerNetErrorNotification()
  30. }
  31. //MARK: Public Method
  32. //防止异常滚动
  33. func reloadDataWithPreserveScrollPosition() {
  34. // 记录当前可见的 section 和 row
  35. if let indexPaths = tableView.indexPathsForVisibleRows,
  36. let firstIndexPath = indexPaths.first {
  37. visibleIndexPath = firstIndexPath
  38. }
  39. // 调用 reloadData()
  40. tableView.reloadData()
  41. // 手动滚动到之前记录的 section 和 row
  42. if let indexPath = visibleIndexPath {
  43. tableView.scrollToRow(at: indexPath, at: .top, animated: false)
  44. }
  45. }
  46. func startRefresh() {
  47. self.tableView.setContentOffset(.zero, animated: true)
  48. DispatchQueue.delay(time: 0.3, action: {
  49. self.tableView.es.startPullToRefresh()
  50. })
  51. }
  52. func registerNetErrorNotification() {
  53. NotificationCenter.default.rx.notification(custom: .netError)
  54. .subscribe(onNext: {[unowned self] (_) in
  55. self.stopRefresh()
  56. self.showNetErrorView()
  57. })
  58. .disposed(by: disposeBag)
  59. }
  60. func showNetErrorView() {
  61. if totalItems() > 0 { return }
  62. self.showAnimationView(self.tableView, animationType: .failure)
  63. }
  64. func showLoadAnimation() {
  65. self.showAnimationView(self.tableView)
  66. }
  67. func hideLoadAnimation() {
  68. self.hideAnimationView(self.tableView)
  69. }
  70. func stopRefresh() {
  71. guard let isRefreshing = tableView.header?.isRefreshing else { return }
  72. if isRefreshing {
  73. tableView.es.stopPullToRefresh()
  74. }
  75. }
  76. //MARK: Private Method
  77. func stopLoad() {
  78. guard let isLoading = tableView.footer?.isRefreshing else { return }
  79. if isLoading {
  80. tableView.es.stopLoadingMore()
  81. }
  82. }
  83. }
  84. //MARK: Notification
  85. extension BaseGroupTableViewController {
  86. func totalItems() -> Int {
  87. var totalItems: Int = 0
  88. let sectionCount = self.tableView.numberOfSections
  89. for i in 0 ..< sectionCount {
  90. let items = self.tableView.numberOfRows(inSection: i)
  91. totalItems += items
  92. }
  93. return totalItems
  94. }
  95. }
  96. extension BaseGroupTableViewController:EmptyDelegate,EmptyDataSource {
  97. func customViewForEmpty(in view: UIView) -> UIView? {
  98. return EmptyView.loadFromNib()
  99. }
  100. func emptyShouldAllowScroll(in view: UIView) -> Bool {
  101. return true
  102. }
  103. func emptyShouldDisplay(in view: UIView) -> Bool {
  104. return isEmptyDisplay
  105. }
  106. }