JCGroupMembersViewController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // JCGroupMembersViewController.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/5/10.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. import RxSwift
  11. import RxCocoa
  12. class JCGroupMembersViewController: UIViewController {
  13. var group: JMSGGroup!
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. _init()
  17. }
  18. fileprivate lazy var searchController: JCSearchController = JCSearchController(searchResultsController: nil)
  19. fileprivate lazy var searchView: UIView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.width, height: 36))
  20. private var collectionView: UICollectionView!
  21. fileprivate var count = 0
  22. fileprivate var sectionCount = 0
  23. fileprivate lazy var users: [JMSGUser] = []
  24. fileprivate lazy var filteredUsersArray: [JMSGUser] = []
  25. let disposeBag = DisposeBag()
  26. private func _init() {
  27. self.title = "群成员"
  28. view.backgroundColor = .white
  29. definesPresentationContext = true
  30. users = group.memberArray()
  31. filteredUsersArray = users
  32. count = filteredUsersArray.count
  33. searchView.backgroundColor = UIColor(netHex: 0xe8edf3)
  34. // searchController.searchBar.delegate = self
  35. searchController.hidesNavigationBarDuringPresentation = false
  36. searchView.addSubview(searchController.searchBar)
  37. let flowLayout = UICollectionViewFlowLayout()
  38. flowLayout.scrollDirection = .vertical
  39. flowLayout.minimumInteritemSpacing = 0
  40. flowLayout.minimumLineSpacing = 0
  41. flowLayout.headerReferenceSize = CGSize(width: view.width, height: 36)
  42. collectionView = UICollectionView(frame: view.frame, collectionViewLayout: flowLayout)
  43. collectionView.bounces = true
  44. collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "kHeaderView")
  45. collectionView.backgroundColor = .white
  46. collectionView.delegate = self
  47. collectionView.dataSource = self
  48. collectionView.register(JCGroupMemberCell.self, forCellWithReuseIdentifier: "JCGroupMemberCell")
  49. view.addSubview(collectionView)
  50. _ = searchController.searchBar.rx.text.orEmpty
  51. .throttle(0.5, scheduler: MainScheduler.instance).subscribe(onNext: { (text) in
  52. self.filter(text)
  53. })
  54. .disposed(by: disposeBag)
  55. _ = searchController.searchBar.rx.cancelButtonClicked.subscribe(onNext: { (_) in
  56. self.filter("")
  57. }).disposed(by: disposeBag)
  58. }
  59. fileprivate func filter(_ searchString: String) {
  60. if searchString.isEmpty || searchString == "" {
  61. filteredUsersArray = users
  62. collectionView.reloadData()
  63. return
  64. }
  65. filteredUsersArray = _JCFilterUsers(users: users, string: searchString)
  66. collectionView.reloadData()
  67. }
  68. }
  69. extension JCGroupMembersViewController: UICollectionViewDelegate, UICollectionViewDataSource {
  70. func numberOfSections(in collectionView: UICollectionView) -> Int {
  71. return 1
  72. }
  73. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  74. return filteredUsersArray.count
  75. }
  76. func collectionView(_ collectionView: UICollectionView,
  77. layout collectionViewLayout: UICollectionViewLayout,
  78. sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
  79. return CGSize(width:Int(collectionView.frame.size.width / 5), height: 90)
  80. }
  81. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  82. return collectionView.dequeueReusableCell(withReuseIdentifier: "JCGroupMemberCell", for: indexPath)
  83. }
  84. func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  85. guard let cell = cell as? JCGroupMemberCell else {
  86. return
  87. }
  88. cell.backgroundColor = .white
  89. cell.bindDate(user: filteredUsersArray[indexPath.row])
  90. }
  91. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  92. let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "kHeaderView", for: indexPath)
  93. if kind == UICollectionView.elementKindSectionHeader {
  94. header.backgroundColor = UIColor(netHex: 0xe8edf3)
  95. header.addSubview(searchView)
  96. }
  97. return header
  98. }
  99. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  100. let user = filteredUsersArray[indexPath.row]
  101. let vc = JCUserInfoViewController()
  102. vc.user = user
  103. navigationController?.pushViewController(vc, animated: true)
  104. searchController.isActive = false
  105. filter("")
  106. }
  107. }
  108. //extension JCGroupMembersViewController: UISearchBarDelegate {
  109. // func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  110. // filter(searchText)
  111. // }
  112. //
  113. // func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  114. // filter("")
  115. // }
  116. //}