SwitchAccountViewController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // SwitchAccountViewController.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/7/28.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. final class SwitchAccountViewController: BaseTableViewController {
  11. let cellIdentifier:String = "_SwitchAccountCellID"
  12. // MARK: UI let
  13. let statusBar = UIView().then {
  14. $0.backgroundColor = UIColor.db_theme
  15. }
  16. let navBar = MeTraineeNavBar.loadFromNib().then {
  17. $0.search_layout_right.constant = 15.f
  18. }
  19. //edit
  20. var edit:Bool = false
  21. //切换viewmodel
  22. let switchAccountViewModel = SwitchAccountViewModel()
  23. override func setupConstraints() {
  24. statusBar.snp.makeConstraints { (make) in
  25. make.left.right.top.equalToSuperview()
  26. make.height.equalTo(Metric.statusBarHeight)
  27. }
  28. navBar.snp.remakeConstraints { (make) in
  29. make.left.right.equalToSuperview()
  30. make.height.equalTo(Metric.navBarHeight)
  31. make.top.equalTo(statusBar.snp.bottom)
  32. }
  33. self.tableView.snp.remakeConstraints { make in
  34. make.top.equalTo(navBar.snp.bottom)
  35. make.left.right.bottom.equalTo(self.view)
  36. }
  37. self.loadViewIfNeeded()
  38. }
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. setupUI()
  42. biandView()
  43. }
  44. // MARK: 私有方法
  45. func setupUI(){
  46. self.view.addSubview(navBar)
  47. self.view.addSubview(statusBar)
  48. self.navBar.title_label.text = "个人中心"
  49. self.view.backgroundColor = .db_theme
  50. self.navBar.back_button.addTarget(self, action: #selector(actionBackdo), for: .touchUpInside)
  51. self.navBar.search_button.setImage(nil, for: .normal)
  52. self.navBar.search_button.setTitle("编辑", for: .normal)
  53. self.navBar.search_button.titleLabel?.font = NYFont.SysFont.sys_14
  54. self.switchAccountViewModel.vc = self
  55. }
  56. //绑定-rx
  57. func biandView(){
  58. tableView.delegate = self.switchAccountViewModel
  59. tableView.separatorStyle = .none //去除分割线
  60. tableView.register(UINib(nibName: "SwitchAccountCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
  61. tableView.rowHeight = 96.f
  62. tableView.alwaysBounceVertical = false
  63. tableView.bounces = false
  64. tableView.contentInset = UIEdgeInsets(top: 20, left: 0, bottom: 20, right: 0)
  65. self.isEmptyDisplay = true
  66. self.hideLoadAnimation()
  67. //编辑事件
  68. self.navBar.search_button.rx.tap.subscribe ({ [unowned self] (_) in
  69. self.edit = !self.edit
  70. let title = self.edit ? "完成":"编辑"
  71. self.navBar.search_button.setTitle(title, for: .normal)
  72. self.switchAccountViewModel.getAccountsRead()
  73. }).disposed(by: disposeBag)
  74. //RX 绑定
  75. self.switchAccountViewModel.items
  76. .bind(to: tableView.rx.items(cellIdentifier: cellIdentifier,cellType:SwitchAccountCell.self)) {[unowned self] (row, model, cell) in
  77. cell.sacontentView.backgroundColor = ((row%2) != 0) ? UIColor("#113357") : UIColor("#0B2B4D")
  78. cell.setIsEdit(edit: self.edit)
  79. cell.setUserinfo(userinfo: model)
  80. cell.index = row
  81. cell.viewModel = self.switchAccountViewModel
  82. }
  83. .disposed(by: disposeBag)
  84. // 单独处理删除按钮的点击事件,不与 tableView.rx.items 的绑定混合
  85. self.switchAccountViewModel.deleteButtonTapped
  86. .subscribe(onNext: { [unowned self] row in
  87. // 在这里处理点击事件,row 表示点击的单元格的索引
  88. self.switchAccountViewModel.deleteAccount(row: row)
  89. })
  90. .disposed(by: disposeBag)
  91. // tableView点击事件
  92. tableView.rx.itemSelected.throttle(.seconds(1), scheduler: MainScheduler.instance)
  93. .subscribe(onNext: { [unowned self]indexPath in
  94. print("点击\(indexPath)行")
  95. if !self.edit{
  96. self.switchAccountViewModel.switchAccount(row: indexPath.row)
  97. }
  98. }).disposed(by: disposeBag)
  99. self.switchAccountViewModel.getAccountsRead()
  100. }
  101. }