JCGroupSettingCell.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // JCGroupSettingCell.swift
  3. // JChat
  4. //
  5. // Created by deng on 2017/4/27.
  6. // Copyright © 2017年 HXHG. All rights reserved.
  7. //
  8. import UIKit
  9. import JMessage
  10. @objc public protocol JCGroupSettingCellDelegate: NSObjectProtocol {
  11. @objc optional func clickMoreButton(clickButton button: UIButton)
  12. @objc optional func clickAddCell(cell: JCGroupSettingCell)
  13. @objc optional func clickRemoveCell(cell: JCGroupSettingCell)
  14. @objc optional func didSelectCell(cell: JCGroupSettingCell, indexPath: IndexPath)
  15. }
  16. public class JCGroupSettingCell: UITableViewCell {
  17. weak var delegate: JCGroupSettingCellDelegate?
  18. var group: JMSGGroup!
  19. convenience init(style: UITableViewCell.CellStyle, reuseIdentifier: String?, group: JMSGGroup) {
  20. self.init(style: style, reuseIdentifier: reuseIdentifier)
  21. self.group = group
  22. _init()
  23. }
  24. override public func awakeFromNib() {
  25. super.awakeFromNib()
  26. _init()
  27. }
  28. private lazy var moreButton: UIButton = UIButton()
  29. fileprivate var count = 0
  30. fileprivate var sectionCount = 0
  31. fileprivate lazy var users: [JMSGUser] = []
  32. fileprivate var isMyGroup = false
  33. fileprivate var currentUserCount = 0
  34. private lazy var flowLayout: UICollectionViewFlowLayout = {
  35. let flowLayout = UICollectionViewFlowLayout()
  36. flowLayout.scrollDirection = .vertical
  37. flowLayout.minimumInteritemSpacing = 0
  38. flowLayout.minimumLineSpacing = 0
  39. return flowLayout
  40. }()
  41. private lazy var collectionView: UICollectionView = {
  42. let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: self.flowLayout)
  43. collectionView.delegate = self
  44. collectionView.dataSource = self
  45. collectionView.register(JCGroupMemberCell.self, forCellWithReuseIdentifier: "JCGroupMemberCell")
  46. collectionView.isScrollEnabled = false
  47. collectionView.backgroundColor = UIColor.clear
  48. return collectionView
  49. }()
  50. func bindData(_ group: JMSGGroup) {
  51. self.group = group
  52. _getData()
  53. self.collectionView.reloadData()
  54. }
  55. private func _init() {
  56. _getData()
  57. addSubview(collectionView)
  58. let showMore = isMyGroup ? count > 13 : count > 14
  59. if showMore {
  60. moreButton.addTarget(self, action: #selector(_clickMore), for: .touchUpInside)
  61. moreButton.setTitleColor(UIColor(netHex: 0x999999), for: .normal)
  62. moreButton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
  63. moreButton.setTitle("查看更多 >", for: .normal)
  64. self.addSubview(moreButton)
  65. addConstraint(_JCLayoutConstraintMake(moreButton, .centerX, .equal, self, .centerX))
  66. addConstraint(_JCLayoutConstraintMake(moreButton, .width, .equal, self, .width))
  67. addConstraint(_JCLayoutConstraintMake(moreButton, .height, .equal, nil, .notAnAttribute, 26))
  68. addConstraint(_JCLayoutConstraintMake(moreButton, .bottom, .equal, self, .bottom, -14))
  69. }
  70. addConstraint(_JCLayoutConstraintMake(collectionView, .left, .equal, self, .left, 15))
  71. addConstraint(_JCLayoutConstraintMake(collectionView, .right, .equal, self, .right, -15))
  72. addConstraint(_JCLayoutConstraintMake(collectionView, .top, .equal, self, .top))
  73. if isMyGroup {
  74. if count > 8 {
  75. addConstraint(_JCLayoutConstraintMake(collectionView, .height, .equal, nil, .notAnAttribute, 260))
  76. } else if count > 3 {
  77. addConstraint(_JCLayoutConstraintMake(collectionView, .height, .equal, nil, .notAnAttribute, 200))
  78. } else {
  79. addConstraint(_JCLayoutConstraintMake(collectionView, .height, .equal, nil, .notAnAttribute, 100))
  80. }
  81. } else {
  82. if count > 9 {
  83. addConstraint(_JCLayoutConstraintMake(collectionView, .height, .equal, nil, .notAnAttribute, 260))
  84. } else if count > 4 {
  85. addConstraint(_JCLayoutConstraintMake(collectionView, .height, .equal, nil, .notAnAttribute, 200))
  86. } else {
  87. addConstraint(_JCLayoutConstraintMake(collectionView, .height, .equal, nil, .notAnAttribute, 100))
  88. }
  89. }
  90. }
  91. @objc func _clickMore() {
  92. delegate?.clickMoreButton?(clickButton: moreButton)
  93. }
  94. fileprivate func _getData() {
  95. users = group.memberArray()
  96. currentUserCount = users.count
  97. let user = JMSGUser.myInfo()
  98. // && group.ownerAppKey == user.appKey! 这里group.ownerAppKey == "" 目测sdk bug
  99. if group.owner == user.username {
  100. isMyGroup = true
  101. }
  102. count = users.count
  103. if isMyGroup {
  104. if count > 13 {
  105. currentUserCount = 13
  106. }
  107. if count > 8 {
  108. sectionCount = 3
  109. } else if count > 3 {
  110. sectionCount = 2
  111. } else {
  112. sectionCount = 1
  113. }
  114. } else {
  115. if count > 14 {
  116. currentUserCount = 14
  117. }
  118. if count > 9 {
  119. sectionCount = 3
  120. } else if count > 4 {
  121. sectionCount = 2
  122. } else {
  123. sectionCount = 1
  124. }
  125. }
  126. }
  127. }
  128. extension JCGroupSettingCell: UICollectionViewDelegate, UICollectionViewDataSource {
  129. public func numberOfSections(in collectionView: UICollectionView) -> Int {
  130. return sectionCount
  131. }
  132. public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  133. if isMyGroup {
  134. if section == 0 {
  135. return count >= 3 ? 5 : count + 2
  136. }
  137. if section == 1 {
  138. return count >= 8 ? 5 : (count - 3)
  139. }
  140. return count >= 13 ? 5 : count - 8
  141. }
  142. if section == 0 {
  143. return count >= 4 ? 5 : count + 1
  144. }
  145. if section == 1 {
  146. return count >= 9 ? 5 : (count - 4)
  147. }
  148. return count >= 14 ? 5 : count - 9
  149. }
  150. func collectionView(_ collectionView: UICollectionView,
  151. layout collectionViewLayout: UICollectionViewLayout,
  152. sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
  153. return CGSize(width:Int(collectionView.frame.size.width / 5), height: Int(collectionView.frame.size.height / CGFloat(sectionCount)))
  154. }
  155. public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  156. return collectionView.dequeueReusableCell(withReuseIdentifier: "JCGroupMemberCell", for: indexPath)
  157. }
  158. public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  159. guard let cell = cell as? JCGroupMemberCell else {
  160. return
  161. }
  162. let index = indexPath.section * 5 + indexPath.row
  163. if index == currentUserCount {
  164. cell.avator = UIImage.loadImage("com_icon_single_add")
  165. return
  166. }
  167. if index == currentUserCount + 1 {
  168. cell.avator = UIImage.loadImage("com_icon_remove")
  169. return
  170. }
  171. cell.bindDate(user: users[index])
  172. }
  173. public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  174. let index = indexPath.section * 5 + indexPath.row
  175. if index == currentUserCount {
  176. delegate?.clickAddCell?(cell: self)
  177. return
  178. }
  179. if index == currentUserCount + 1 {
  180. delegate?.clickRemoveCell?(cell: self)
  181. return
  182. }
  183. delegate?.didSelectCell?(cell: self, indexPath: indexPath)
  184. }
  185. }