// // BaseGroupTableViewController.swift // JiaPeiManage // // Created by Ning.ge on 2023/6/21. // import UIKit import RxSwift import EmptyKit class BaseGroupTableViewController: BaseViewController,Refreshable { var isEmptyDisplay: Bool = true var isNotData:Bool = false var visibleIndexPath: IndexPath? let tableView: UITableView = UITableView(frame: CGRectZero, style: .grouped).then{ $0.backgroundColor = .db_theme $0.showsVerticalScrollIndicator = false $0.tableFooterView = UIView() } override func setupConstraints() { tableView.snp.makeConstraints { (make) in make.edges.equalTo(0) } } override func viewDidLoad() { if isNotData { emptyView.loading_imageview.image = UIImage(named: "animation_loading_error_2") emptyView.loading_label.text = "暂无数据" } super.viewDidLoad() tableView.ept.dataSource = self tableView.ept.delegate = self view.addSubview(tableView) showLoadAnimation() registerNetErrorNotification() } //MARK: Public Method //防止异常滚动 func reloadDataWithPreserveScrollPosition() { // 记录当前可见的 section 和 row if let indexPaths = tableView.indexPathsForVisibleRows, let firstIndexPath = indexPaths.first { visibleIndexPath = firstIndexPath } // 调用 reloadData() tableView.reloadData() // 手动滚动到之前记录的 section 和 row if let indexPath = visibleIndexPath { tableView.scrollToRow(at: indexPath, at: .top, animated: false) } } func startRefresh() { self.tableView.setContentOffset(.zero, animated: true) DispatchQueue.delay(time: 0.3, action: { self.tableView.es.startPullToRefresh() }) } func registerNetErrorNotification() { NotificationCenter.default.rx.notification(custom: .netError) .subscribe(onNext: {[unowned self] (_) in self.stopRefresh() self.showNetErrorView() }) .disposed(by: disposeBag) } func showNetErrorView() { if totalItems() > 0 { return } self.showAnimationView(self.tableView, animationType: .failure) } func showLoadAnimation() { self.showAnimationView(self.tableView) } func hideLoadAnimation() { self.hideAnimationView(self.tableView) } func stopRefresh() { guard let isRefreshing = tableView.header?.isRefreshing else { return } if isRefreshing { tableView.es.stopPullToRefresh() } } //MARK: Private Method func stopLoad() { guard let isLoading = tableView.footer?.isRefreshing else { return } if isLoading { tableView.es.stopLoadingMore() } } } //MARK: Notification extension BaseGroupTableViewController { func totalItems() -> Int { var totalItems: Int = 0 let sectionCount = self.tableView.numberOfSections for i in 0 ..< sectionCount { let items = self.tableView.numberOfRows(inSection: i) totalItems += items } return totalItems } } extension BaseGroupTableViewController:EmptyDelegate,EmptyDataSource { func customViewForEmpty(in view: UIView) -> UIView? { return EmptyView.loadFromNib() } func emptyShouldAllowScroll(in view: UIView) -> Bool { return true } func emptyShouldDisplay(in view: UIView) -> Bool { return isEmptyDisplay } }