Pārlūkot izejas kodu

优化 统计 page

openlockPPP 1 gadu atpakaļ
vecāks
revīzija
ebe6193bef

+ 2 - 2
JiaPeiManage.xcodeproj/project.pbxproj

@@ -1475,7 +1475,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
 				CODE_SIGN_STYLE = Automatic;
-				CURRENT_PROJECT_VERSION = 1202023071902;
+				CURRENT_PROJECT_VERSION = 1202023071904;
 				DEVELOPMENT_TEAM = K7T6CU5SW5;
 				GENERATE_INFOPLIST_FILE = YES;
 				INFOPLIST_FILE = JiaPeiManage/Info.plist;
@@ -1515,7 +1515,7 @@
 				ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
 				ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
 				CODE_SIGN_STYLE = Automatic;
-				CURRENT_PROJECT_VERSION = 1202023071902;
+				CURRENT_PROJECT_VERSION = 1202023071904;
 				DEVELOPMENT_TEAM = K7T6CU5SW5;
 				GENERATE_INFOPLIST_FILE = YES;
 				INFOPLIST_FILE = JiaPeiManage/Info.plist;

+ 19 - 1
JiaPeiManage/Sources/Main/Base/BaseGroupTableViewController.swift

@@ -13,7 +13,7 @@ import EmptyKit
 class BaseGroupTableViewController: BaseViewController,Refreshable {
     
     var isEmptyDisplay: Bool = true
-    
+    var visibleIndexPath: IndexPath?
     let tableView: UITableView = UITableView(frame: CGRectZero, style: .grouped).then{
         $0.backgroundColor = .db_theme
         $0.showsVerticalScrollIndicator = false
@@ -42,6 +42,24 @@ class BaseGroupTableViewController: BaseViewController,Refreshable {
     }
     
     //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)

+ 7 - 7
JiaPeiManage/Sources/Modulars/Statistics/ViewModel/StatisticsPageViewModel.swift

@@ -41,7 +41,7 @@ class StatisticsPageViewModel: NSObject {
                         self.stuCountDataModel = stuCountDataModel
 //                        self.itemsSubject.onNext(stuCountDataModel.rows)//消息rx
                     self.vc!.hideLoadAnimation()
-                        self.vc!.tableView.reloadData()
+                        self.vc!.reloadDataWithPreserveScrollPosition()
                         self.vc!.stopRefresh()
                         NYTips.hide()
                     }, onError: { error in
@@ -63,8 +63,8 @@ class StatisticsPageViewModel: NSObject {
         case .day:
             // 发起请求,获取日数据
             self.statisticsService.everyDayStuCountRequest(city: LocalManager.userInfo.city!, schoolId: LocalManager.userInfo.schoolId, startDate: self.startDate, endDate: self.endDate, fieldType: self.fieldType,sortType: self.sortType).subscribe(onSuccess: { stuCountDataModel in
-                    self.stuCountDataModel = stuCountDataModel
-                self.vc!.tableView.reloadData()
+                self.stuCountDataModel = stuCountDataModel
+                self.vc!.reloadDataWithPreserveScrollPosition()
                 }, onError: { error in
                 })
             .disposed(by: self.vc!.disposeBag)
@@ -73,8 +73,8 @@ class StatisticsPageViewModel: NSObject {
             let startMonth = String(self.startDate.prefix(7))
             let endMonth =  String(self.endDate.prefix(7))
             self.statisticsService.everyMonthStuCount(city: LocalManager.userInfo.city!, schoolId: LocalManager.userInfo.schoolId, startMonth: startMonth, endMonth: endMonth, fieldType: self.fieldType,sortType: self.sortType).subscribe(onSuccess: { stuCountDataModel in
-                    self.stuCountDataModel = stuCountDataModel
-                self.vc!.tableView.reloadData()
+                self.stuCountDataModel = stuCountDataModel
+                self.vc!.reloadDataWithPreserveScrollPosition()
                 }, onError: { error in
                 })
             .disposed(by: self.vc!.disposeBag)
@@ -83,8 +83,8 @@ class StatisticsPageViewModel: NSObject {
             let startYear = String(self.startDate.prefix(4))
             let endYear =  String(self.endDate.prefix(4))
             self.statisticsService.everyYearStuCount(city: LocalManager.userInfo.city!, schoolId: LocalManager.userInfo.schoolId, startYear: startYear, endYear: endYear, fieldType: self.fieldType,sortType: self.sortType).subscribe(onSuccess: { stuCountDataModel in
-                    self.stuCountDataModel = stuCountDataModel
-                self.vc!.tableView.reloadData()
+                self.stuCountDataModel = stuCountDataModel
+                self.vc!.reloadDataWithPreserveScrollPosition()
                 }, onError: { error in
                 })
             .disposed(by: self.vc!.disposeBag)