OOAttandanceWorkPlaceController.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // OOAttandanceWorkPlaceController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/5/21.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. class OOAttandanceWorkPlaceController: UITableViewController {
  10. private lazy var viewModel:OOAttandanceViewModel = {
  11. return OOAttandanceViewModel()
  12. }()
  13. private var models:[OOAttandanceWorkPlace] = []
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. title = "地点管理"
  17. tableView.register(UINib.init(nibName: "OOAttandanceWorkPlaceCell", bundle: nil), forCellReuseIdentifier: "OOAttandanceWorkPlaceCell")
  18. tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
  19. self.loadWorkPlace()
  20. })
  21. loadWorkPlace()
  22. }
  23. private func loadWorkPlace() {
  24. self.showLoading()
  25. models.removeAll()
  26. viewModel.getLocationWorkPlace { (result) in
  27. switch result {
  28. case .ok(let item):
  29. let items = item as? [OOAttandanceWorkPlace]
  30. DispatchQueue.main.async {
  31. self.hideLoading()
  32. items?.forEach({ (place) in
  33. self.models.append(place)
  34. })
  35. }
  36. break
  37. case .fail(let errorMessage):
  38. DispatchQueue.main.async {
  39. self.showError(title: "读取位置列表出错\n,\(errorMessage)" )
  40. }
  41. break
  42. default:
  43. break
  44. }
  45. DispatchQueue.main.async {
  46. if self.tableView.mj_header.isRefreshing() {
  47. self.tableView.mj_header.endRefreshing()
  48. }
  49. self.tableView.reloadData()
  50. }
  51. }
  52. }
  53. override func didReceiveMemoryWarning() {
  54. super.didReceiveMemoryWarning()
  55. }
  56. // MARK: - Table view data source
  57. override func numberOfSections(in tableView: UITableView) -> Int {
  58. return 1
  59. }
  60. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  61. return models.count
  62. }
  63. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  64. let cell = tableView.dequeueReusableCell(withIdentifier: "OOAttandanceWorkPlaceCell", for: indexPath) as! (OOAttandanceWorkPlaceCell & Configurable)
  65. let item = models[indexPath.row]
  66. cell.config(withItem: item)
  67. return cell
  68. }
  69. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  70. return true
  71. }
  72. override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  73. let action1 = UITableViewRowAction(style: .destructive, title: "删除") { (action, indexPath) in
  74. self._delete(indexPath)
  75. }
  76. return [action1]
  77. }
  78. private func _delete(_ indexPath:IndexPath){
  79. let item = models[indexPath.row]
  80. self.showLoading(title: "删除中,请稍候...")
  81. viewModel.deleteLocationWorkPlace(item) { (resultType) in
  82. self.hideLoading()
  83. switch resultType {
  84. case .reload:
  85. DispatchQueue.main.async {
  86. self.models.remove(at: indexPath.row)
  87. self.tableView.reloadData()
  88. }
  89. break
  90. case .fail(let errorMessage):
  91. DispatchQueue.main.async {
  92. self.showError(title: "删除不成功\n\(errorMessage)" )
  93. self.tableView.reloadData()
  94. }
  95. default:
  96. break
  97. }
  98. }
  99. }
  100. }