ContactPersonPickerViewController.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // ContactPersonPickerViewController.swift
  3. // O2Platform
  4. //
  5. // Created by FancyLou on 2019/8/12.
  6. // Copyright © 2019 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import CocoaLumberjack
  10. class ContactPersonPickerViewController: UITableViewController {
  11. private var personDataList:[OOPersonModel] = []
  12. private let viewModel: ContactPickerViewModel = {
  13. return ContactPickerViewModel()
  14. }()
  15. private let searchController = UISearchController(searchResultsController: nil)
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.searchController.delegate = self
  19. self.searchController.searchResultsUpdater = self
  20. self.searchController.searchBar.delegate = self
  21. self.searchBarInit(searchController.searchBar)
  22. self.definesPresentationContext = true
  23. self.searchController.dimsBackgroundDuringPresentation = false
  24. self.searchController.hidesNavigationBarDuringPresentation = false
  25. // Setup the Scope Bar
  26. self.tableView.tableHeaderView = searchController.searchBar
  27. self.tableView.tableHeaderView?.sizeToFit()
  28. }
  29. // MARK: - Table view data source
  30. override func numberOfSections(in tableView: UITableView) -> Int {
  31. return 1
  32. }
  33. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  34. return self.personDataList.count
  35. }
  36. override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  37. return 10
  38. }
  39. override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  40. return 3
  41. }
  42. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  43. let cell = tableView.dequeueReusableCell(withIdentifier: "personPickerViewCell", for: indexPath) as! PersonPickerTableViewCell
  44. let person = self.personDataList[indexPath.row]
  45. cell.loadPersonInfo(info: person, checked: self.isSelected(value: person.distinguishedName!))
  46. return cell
  47. }
  48. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  49. let value = self.personDataList[indexPath.row].distinguishedName!
  50. let name = self.personDataList[indexPath.row].name!
  51. if self.isSelected(value: value) {
  52. self.removeSelected(value: value)
  53. }else {
  54. self.addSelected(value: value, name: name)
  55. }
  56. self.tableView.reloadRows(at: [indexPath], with: .automatic)
  57. self.tableView.deselectRow(at: indexPath, animated: false)
  58. }
  59. /*
  60. // MARK: - Navigation
  61. // In a storyboard-based application, you will often want to do a little preparation before navigation
  62. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  63. // Get the new view controller using segue.destination.
  64. // Pass the selected object to the new view controller.
  65. }
  66. */
  67. //MARK: - private method
  68. func loadSearchData(_ searchText:String?,scopeIndex:Int){
  69. if (searchText == nil || searchText?.isEmpty == true) {
  70. self.personDataList.removeAll()
  71. self.tableView.reloadData()
  72. }else{
  73. self.showLoading()
  74. viewModel.searchPersonList(searchText: searchText!).then { (list) in
  75. self.personDataList.removeAll()
  76. self.personDataList = list
  77. self.tableView.reloadData()
  78. self.hideLoading()
  79. }.catch { (error) in
  80. DDLogError(error.localizedDescription)
  81. self.hideLoading()
  82. }
  83. }
  84. }
  85. private func isSelected(value: String) -> Bool {
  86. if let vc = self.parent as? ContactPickerViewController {
  87. return vc.isSelectedValue(type: .person, value: value)
  88. }
  89. return false
  90. }
  91. private func removeSelected(value: String) {
  92. if let vc = self.parent as? ContactPickerViewController {
  93. vc.removeSelectedValue(type: .person, value: value)
  94. }
  95. }
  96. private func addSelected(value: String, name: String) {
  97. }
  98. private func searchBarInit(_ searchBar:UISearchBar){
  99. if let searchField = searchBar.value(forKey: "searchField") as? UITextField {
  100. searchField.placeholder = "请输入搜索关键字"
  101. }
  102. UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "取消"
  103. }
  104. }
  105. extension ContactPersonPickerViewController:UISearchControllerDelegate{
  106. func willPresentSearchController(_ searchController: UISearchController) {
  107. NSLog("willPresentSearchController")
  108. }
  109. func didPresentSearchController(_ searchController: UISearchController) {
  110. NSLog("didPresentSearchController")
  111. searchController.searchBar.setShowsCancelButton(false, animated: true)
  112. }
  113. func willDismissSearchController(_ searchController: UISearchController) {
  114. NSLog("willDismissSearchController")
  115. }
  116. func didDismissSearchController(_ searchController: UISearchController) {
  117. NSLog("didDismissSearchController")
  118. }
  119. func presentSearchController(_ searchController: UISearchController) {
  120. NSLog("presentSearchController")
  121. }
  122. }
  123. extension ContactPersonPickerViewController:UISearchBarDelegate{
  124. func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
  125. self.loadSearchData(searchBar.text, scopeIndex: selectedScope)
  126. }
  127. }
  128. extension ContactPersonPickerViewController:UISearchResultsUpdating{
  129. func updateSearchResults(for searchController: UISearchController) {
  130. let searchBar = searchController.searchBar
  131. self.loadSearchData(searchBar.text, scopeIndex: searchBar.selectedScopeButtonIndex)
  132. }
  133. }