// // NoticeListViewController.swift // Meterage // // Created by 203 on 2023/2/6. // import Alamofire import DefaultsKit import MJRefresh import SwiftyJSON import UIKit class NoticeListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var openSystemNoticeLayout: UIView! @IBOutlet weak var tableView: UITableView! private var pageOffSet: Int = 1 private var isUpdate: Bool = false private var rowsArray: [RowModel] = [] private let defaults = Defaults.shared override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .mainBackground let leftBarBtn = UIBarButtonItem(image: UIImage(systemName: "chevron.backward"), style: .plain, target: self, action: #selector(backToPrevious)) navigationItem.leftBarButtonItem = leftBarBtn navigationItem.title = "消息" navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.mainTextColor] let rightBarBtn = UIBarButtonItem(image: UIImage(named: "read_notice")?.withRenderingMode(.alwaysOriginal), style: .plain, target: self, action: #selector(rightButtonAction)) navigationItem.rightBarButtonItem = rightBarBtn // Do any additional setup after loading the view. tableView.register(UINib(nibName: "NoticeListTableViewCell", bundle: nil), forCellReuseIdentifier: "noticeListTableViewCell") tableView.delegate = self tableView.dataSource = self tableView.separatorColor = .hintColor tableView.backgroundColor = .clear tableView.mj_header = MJRefreshNormalHeader { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in isUpdate = true pageOffSet = 1 loadData(offset: pageOffSet) tableView.mj_header?.endRefreshing() }) } tableView.mj_footer = MJRefreshBackNormalFooter { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: { [self] in isUpdate = false pageOffSet += 1 loadData(offset: pageOffSet) tableView.mj_footer?.endRefreshing() }) } // 默认加载数据 loadData(offset: 1) } func loadData(offset: Int) { let baseURL = defaults.get(for: serverConfigKey)! // 分页参数不能放在httpBody中 let noticeListURL = baseURL + Constant.noticeList.rawValue + "?limit=20&offset=" + String(offset) let token = defaults.get(for: tokenKey) ?? "" let paramDic: [String: Any] = ["noticeNo": "", "noticeTitle": "", "noticePublisher": "", "noticeStartTime": "", "noticeEndTime": ""] Alamofire.request(HttpRequestCreator.shared.createPostRequest(url: noticeListURL, dic: paramDic, token: token)).responseJSON { [self] response in switch response.result { case let .success(value): let inspectModel = NoticeListModel(respJson: JSON(value)) if inspectModel.code == 200 { if isUpdate { rowsArray = inspectModel.data.rows } else { inspectModel.data.rows.forEach { model in rowsArray.append(model) } } // 必须要重新加载数据,否则不走数据绑定 tableView.reloadData() } else { AlertHub.shared.showWaringAlert(controller: self, message: "数据加载失败,请重试") } case .failure: AlertHub.shared.showWaringAlert(controller: self, message: "未知错误") } } } //返回按钮点击事件 @objc func backToPrevious() { dismiss(animated: true) } //右边按钮点击事件 @objc func rightButtonAction() { let alertController = UIAlertController(title: "提示", message: "确定将所有未读消息标为已读?", preferredStyle: .alert) let cancelAction = UIAlertAction(title: "取消", style: .destructive, handler: nil) let okAction = UIAlertAction(title: "确定", style: .default, handler: { action in print("点击了确定") }) alertController.addAction(cancelAction) alertController.addAction(okAction) present(alertController, animated: true, completion: nil) } @IBAction func closeNoticeLayout(_ sender: Any) { openSystemNoticeLayout.isHidden = true } /// UITableView func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 60 } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { // let mapViewController = InspectMapViewController(nibName: "InspectMapViewController", bundle: nil) // // 委托代理 // valueDelegate = mapViewController // // 实现代理的方法,传值 // let row = (indexPath as NSIndexPath).row // let model = rowsArray[row] // valueDelegate.transfer(controller: self, dic: ["id": model.id]) // navigationController?.pushViewController(mapViewController, animated: true) } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { rowsArray.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell: NoticeListTableViewCell = tableView.dequeueReusableCell(withIdentifier: "noticeListTableViewCell", for: indexPath) as! NoticeListTableViewCell let row = (indexPath as NSIndexPath).row let model = rowsArray[row] cell.noticeTitleView.text = model.noticeTitle cell.noticeTimeView.text = model.noticeTime cell.noticeContentView.text = model.noticeSketch //设置分割线上下左右边距 cell.separatorInset = UIEdgeInsets(top: 0, left: 65, bottom: 0, right: 0) cell.selectionStyle = .none return cell } func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) { } }