OrderCommentTableViewCell.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // OrderCommentTableViewCell.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/5/17.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. import IQKeyboardManagerSwift
  11. class OrderCommentTableViewCell: UITableViewCell {
  12. let disposeBag = DisposeBag()
  13. private let maxImageCount: Int = 6
  14. typealias CommentTextViewClosure = (_ text: String) -> Void
  15. var commentTextViewClosure : CommentTextViewClosure?
  16. typealias StartChangedBlock = (_ starValue:Int?) -> Void
  17. var startChangedBlock : StartChangedBlock?
  18. typealias ChoosePicBlock = () -> Void
  19. var choosePicBlock : ChoosePicBlock?
  20. typealias DelPicTransBlock = (_ idxRow:Int?) -> Void
  21. var delPicTransBlock : DelPicTransBlock?
  22. var imgCount: Int = 0
  23. var goodsImageArr: Array<UIImage>? {
  24. didSet {
  25. imgCount = self.goodsImageArr?.count ?? 0
  26. }
  27. }
  28. var orderModelDetailModel: OrderModelDetailModel? {
  29. didSet {
  30. // 商品图片
  31. iconImageView.kf.setImage(with: kURLImage(name: orderModelDetailModel?.productImg ?? "pic_preload"), placeholder: kImage(name: "pic_preload"))
  32. starView.value = CGFloat(self.orderModelDetailModel?.starValue ?? 5)
  33. }
  34. }
  35. class func cellWith(tableView:UITableView,indexPath:IndexPath) -> OrderCommentTableViewCell {
  36. let ID = "OrderCommentTableViewCell"
  37. tableView.register(OrderCommentTableViewCell.self, forCellReuseIdentifier: ID)
  38. let cell : OrderCommentTableViewCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! OrderCommentTableViewCell
  39. cell.indexPath = indexPath
  40. return cell
  41. }
  42. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  43. super.init(style: style, reuseIdentifier: reuseIdentifier)
  44. setupViews()
  45. setupLayouts()
  46. }
  47. required init?(coder aDecoder: NSCoder) {
  48. fatalError("init(coder:) has not been implemented")
  49. }
  50. var indexPath: IndexPath? {
  51. didSet {
  52. }
  53. }
  54. //MRAK: - 设置View
  55. private func setupViews() {
  56. self.selectionStyle = .none
  57. addSubview(cmtBackView)
  58. cmtBackView.addSubview(iconImageView)
  59. cmtBackView.addSubview(titleLabel)
  60. cmtBackView.addSubview(starView)
  61. addSubview(cmtTextView)
  62. addSubview(collectionView)
  63. }
  64. private func setupLayouts() {
  65. cmtBackView.snp.makeConstraints { (make) in
  66. make.top.left.right.equalToSuperview()
  67. make.height.equalTo(91)
  68. }
  69. iconImageView.snp.makeConstraints { (make) in
  70. make.top.equalToSuperview().offset(16)
  71. make.left.equalToSuperview().offset(14)
  72. make.size.equalTo(55)
  73. }
  74. titleLabel.snp.makeConstraints { (make) in
  75. make.left.equalTo(iconImageView.snp_right).offset(10)
  76. make.centerY.equalTo(iconImageView)
  77. // make.width.equalTo(56)
  78. make.height.equalTo(20)
  79. }
  80. starView.snp.makeConstraints { (make) in
  81. make.left.equalTo(titleLabel.snp_right).offset(12)
  82. make.centerY.equalTo(iconImageView)
  83. make.width.equalTo(122)
  84. make.height.equalTo(20)
  85. }
  86. cmtTextView.snp.makeConstraints { (make) in
  87. make.top.equalTo(cmtBackView.snp_bottom)
  88. make.left.equalTo(14)
  89. make.right.equalTo(-14)
  90. make.height.equalTo(100)
  91. }
  92. collectionView.snp.makeConstraints { (make) in
  93. make.top.equalTo(cmtTextView.snp_bottom)
  94. make.left.right.equalToSuperview()
  95. make.height.equalTo(200)
  96. make.bottom.equalToSuperview()
  97. }
  98. }
  99. private lazy var cmtBackView: UIView = {
  100. let cmtBackView = UIView()
  101. return cmtBackView
  102. }()
  103. private lazy var iconImageView: UIImageView = {
  104. let iconImageView = UIImageView()
  105. iconImageView.contentMode = .scaleAspectFit
  106. iconImageView.contentMode = .scaleAspectFill
  107. iconImageView.masksToBounds = true
  108. return iconImageView
  109. }()
  110. private lazy var titleLabel: UILabel = {
  111. let titleLabel = UILabel()
  112. titleLabel.text = "商品评分"
  113. titleLabel.textColor = k333333Color
  114. titleLabel.font = kBoldFont14
  115. titleLabel.textAlignment = .left
  116. titleLabel.numberOfLines = 1
  117. return titleLabel
  118. }()
  119. private lazy var starView: SwiftyStarRatingView = {
  120. let starView = SwiftyStarRatingView()
  121. starView.emptyStarImage = kImage(name: "star_rating_empty")
  122. starView.halfStarImage = kImage(name: "star_rating_half")
  123. starView.filledStarImage = kImage(name: "star_rating_filled")
  124. starView.allowsHalfStars = false
  125. starView.addTarget(self, action: #selector(starViewDidClick), for: .valueChanged)
  126. return starView
  127. }()
  128. private lazy var cmtTextView: IQTextView = {
  129. let cmtTextView = IQTextView()
  130. cmtTextView.backgroundColor = kfafafaColor
  131. cmtTextView.textColor = k333333Color
  132. cmtTextView.font = kRegularFont14
  133. cmtTextView.placeholder = "输入商品评价..."
  134. cmtTextView.placeholderTextColor = k999999Color
  135. cmtTextView.delegate = self
  136. return cmtTextView
  137. }()
  138. private lazy var collectionView: UICollectionView = {
  139. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  140. collectionView.backgroundColor = kffffffColor
  141. collectionView.delegate = self;
  142. collectionView.dataSource = self;
  143. collectionView.showsVerticalScrollIndicator = false
  144. collectionView.showsHorizontalScrollIndicator = false
  145. return collectionView
  146. }()
  147. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  148. let collectionViewLayout = UICollectionViewFlowLayout.init()
  149. collectionViewLayout.minimumLineSpacing = 10
  150. collectionViewLayout.minimumInteritemSpacing = 0
  151. return collectionViewLayout
  152. }()
  153. //加载数据
  154. func reloadData() {
  155. //collectionView重新加载数据
  156. self.collectionView.reloadData()
  157. //更新collectionView的高度约束
  158. let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
  159. collectionView.snp.remakeConstraints { (make) in
  160. make.top.equalTo(cmtTextView.snp_bottom)
  161. make.left.right.equalToSuperview()
  162. make.height.equalTo(contentSize.height)
  163. make.bottom.equalToSuperview()
  164. }
  165. self.collectionView.collectionViewLayout.invalidateLayout()
  166. }
  167. // 评分视图点击
  168. @objc func starViewDidClick(starView: SwiftyStarRatingView) {
  169. let starValue = Int(starView.value)
  170. if let startChangedBlock = self.startChangedBlock {
  171. startChangedBlock(starValue)
  172. }
  173. }
  174. }
  175. extension OrderCommentTableViewCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate {
  176. func numberOfSections(in collectionView: UICollectionView) -> Int {
  177. return 1
  178. }
  179. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  180. if imgCount < maxImageCount {
  181. return imgCount+1
  182. } else {
  183. return maxImageCount
  184. }
  185. }
  186. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  187. if imgCount < maxImageCount && indexPath.row == imgCount {
  188. // 添加图片
  189. let aCell = OrderCommentDefaultCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  190. if imgCount == 0 {
  191. aCell.noteStr = "添加照片"
  192. } else {
  193. aCell.noteStr = "\(imgCount)/\(maxImageCount)"
  194. }
  195. return aCell
  196. } else {
  197. // 展示图片
  198. let pCell = OrderCommentAddImgCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  199. if indexPath.row < imgCount {
  200. pCell.showImage = self.goodsImageArr![indexPath.row]
  201. }
  202. pCell.delPicBlock = {
  203. [weak self] (idxRow) in
  204. if let delPicTransBlock = self?.delPicTransBlock {
  205. delPicTransBlock(idxRow)
  206. }
  207. }
  208. return pCell
  209. }
  210. }
  211. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  212. return CGSize(width:(kScreenWidth-30-5*3)/4, height: (kScreenWidth-30-5*3)/4)
  213. }
  214. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  215. return UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 15)
  216. }
  217. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  218. if imgCount < maxImageCount && indexPath.row == imgCount {
  219. if let choosePicBlock = self.choosePicBlock {
  220. choosePicBlock()
  221. }
  222. }
  223. }
  224. }
  225. extension OrderCommentTableViewCell: UITextViewDelegate {
  226. func textViewDidChange(_ textView: UITextView) {
  227. if textView == cmtTextView {
  228. var fullStr = textView.text ?? ""
  229. if textView.text?.count ?? 0 > 180 {
  230. fullStr = String(fullStr.prefix(150)) as String
  231. textView.text = fullStr
  232. }
  233. if let commentTextViewClosure = self.commentTextViewClosure {
  234. commentTextViewClosure(self.cmtTextView.text ?? "")
  235. }
  236. }
  237. }
  238. }