// // MeTraineeSubjectPageViewController.swift // JiaPeiManage // // Created by Ning.ge on 2023/6/15. // import UIKit import RxSwift import RxCocoa final class MeTraineeSubjectPageViewController: BaseTableViewController { let cellIdentifier:String = "_MeTraineeSubjectCell" // MARK: 服务属性 private let cocahService: CoachServiceType = CoachService(networking: CoachNetworking()) // MARK: UI属性 // MARK: 数据 var traineeDataModel:TraineeDataModel? var rows:[RowInfo] = [] var page:Int = 1 var pageSize:Int = 10 var state = "1" var schoolId:Int = 0 var type:TraineeType? init(type:TraineeType,state:String,schoolId:Int) { self.type = type super.init() self.state = state self.schoolId = schoolId } required convenience init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() getCoachInfoList() //绑定 biandView() } // MARK: 私有方法 func getCoachInfoList() { page = 1 if (self.type == .advancet){ //预报名 self.cocahService.coachTempListRequest(city: LocalManager.userInfo.city!, appCoachId: "", coachId: "\(LocalManager.userInfo.id)", field: "", pageNum: page, pageSize: pageSize, schoolId: schoolId, state: state) .subscribe(onSuccess: { traineeDataModel in self.rows.removeAll() self.traineeDataModel = traineeDataModel self.rows += traineeDataModel.rows! print("coachInfoListRequest成功") self.hideLoadAnimation() self.tableView.reloadData() self.stopRefresh() }, onError: { error in self.stopRefresh() }) .disposed(by: disposeBag) return }else if(self.type == .biandstu){ //绑定学员 self.cocahService.coachInfoListRequest(city: LocalManager.userInfo.city!, appCoachId: "\(LocalManager.userInfo.id)", coachId: "", field: "", pageNum: page, pageSize: pageSize, schoolId: schoolId, state: state) .subscribe(onSuccess: { traineeDataModel in self.rows.removeAll() self.traineeDataModel = traineeDataModel self.rows += traineeDataModel.rows! print("coachInfoListRequest成功") self.hideLoadAnimation() self.tableView.reloadData() self.stopRefresh() NotificationCenter.default.post(name: Notification.updateItemTopTotalNameNotification, object: [traineeDataModel.total,Int(self.state)!-1]) }, onError: { error in self.stopRefresh() }) .disposed(by: disposeBag) return } self.cocahService.coachInfoListRequest(city: LocalManager.userInfo.city!, appCoachId: "", coachId: "\(LocalManager.userInfo.id)", field: "", pageNum: page, pageSize: pageSize, schoolId: schoolId, state: state) .subscribe(onSuccess: { traineeDataModel in self.rows.removeAll() self.traineeDataModel = traineeDataModel self.rows += traineeDataModel.rows! print("coachInfoListRequest成功") self.hideLoadAnimation() self.tableView.reloadData() self.stopRefresh() NotificationCenter.default.post(name: Notification.updateItemTopTotalNameNotification, object: [traineeDataModel.total,Int(self.state)!-1]) }, onError: { error in self.stopRefresh() }) .disposed(by: disposeBag) } //更多 func getLoadMore(){ //计算-页数 if (traineeDataModel != nil){ let total = pageSize*self.page if(total<(traineeDataModel?.total)!){ self.page+=1 }else{ self.stopLoad() return //已经显示完 } } if (self.type == .advancet){ //预报名 self.cocahService.coachTempListRequest(city: LocalManager.userInfo.city!, appCoachId: "", coachId: "\(LocalManager.userInfo.id)", field: "", pageNum: page, pageSize: pageSize, schoolId: schoolId, state: state) .subscribe(onSuccess: { traineeDataModel in self.traineeDataModel = traineeDataModel if(traineeDataModel.rows!.count>0){ self.rows += traineeDataModel.rows! } print("coachInfoListRequest成功") self.tableView.reloadData() self.stopLoad() }, onError: { error in self.stopLoad() }) .disposed(by: disposeBag) return }else if(self.type == .biandstu){ //绑定学员 self.cocahService.coachInfoListRequest(city: LocalManager.userInfo.city!, appCoachId: "\(LocalManager.userInfo.id)", coachId: "", field: "", pageNum: page, pageSize: pageSize, schoolId: schoolId, state: state) .subscribe(onSuccess: { traineeDataModel in self.traineeDataModel = traineeDataModel if(traineeDataModel.rows!.count>0){ self.rows += traineeDataModel.rows! } print("coachInfoListRequest成功") self.tableView.reloadData() self.stopLoad() }, onError: { error in self.stopLoad() }) .disposed(by: disposeBag) return } //请求网络-加载 self.cocahService.coachInfoListRequest(city: LocalManager.userInfo.city!, appCoachId: "", coachId: "\(LocalManager.userInfo.id)", field: "", pageNum: page, pageSize: pageSize, schoolId: schoolId, state: state) .subscribe(onSuccess: { traineeDataModel in self.traineeDataModel = traineeDataModel if(traineeDataModel.rows!.count>0){ self.rows += traineeDataModel.rows! } print("coachInfoListRequest成功") self.tableView.reloadData() self.stopLoad() }, onError: { error in self.stopLoad() }) .disposed(by: disposeBag) } func biandView(){ tableView.delegate = self tableView.dataSource = self tableView.separatorStyle = .none //去除分割线 tableView.register(UINib(nibName: "MeTraineeSubjectCell", bundle: nil), forCellReuseIdentifier: cellIdentifier) tableView.rowHeight = 288.f setupRefreshHeader(tableView) {[unowned self] in self.getCoachInfoList() } self.isEmptyDisplay = false // 设置上拉加载更多 tableView.es.addInfiniteScrolling { [weak self] in self?.getLoadMore() } } } //数据源 extension MeTraineeSubjectPageViewController:UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.rows.count ?? 0 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! MeTraineeSubjectCell cell.index_button.setTitle("\(indexPath.row+1)", for: .normal) cell.setRowInfo(rowInfo: (self.rows[indexPath.row])) return cell } } //事件 extension MeTraineeSubjectPageViewController:UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { tableView.deselectRow(at: indexPath, animated: false) let rowInfo = self.rows[indexPath.row] if rowInfo != nil { let context: Int = rowInfo.id ?? 0 NYSwRouter.push(NYSwPushType.trainee_info,context: context) } } }