OrderCommentTableViewCell.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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.rx.text.orEmpty.changed.subscribe(onNext: {
  136. [weak self] (text) in
  137. self?.cmtTextView.text = String(text.prefix(150)) as String
  138. if let commentTextViewClosure = self?.commentTextViewClosure {
  139. commentTextViewClosure(self?.cmtTextView.text ?? "")
  140. }
  141. }).disposed(by: disposeBag)
  142. return cmtTextView
  143. }()
  144. private lazy var collectionView: UICollectionView = {
  145. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  146. collectionView.backgroundColor = kffffffColor
  147. collectionView.delegate = self;
  148. collectionView.dataSource = self;
  149. collectionView.showsVerticalScrollIndicator = false
  150. collectionView.showsHorizontalScrollIndicator = false
  151. return collectionView
  152. }()
  153. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  154. let collectionViewLayout = UICollectionViewFlowLayout.init()
  155. collectionViewLayout.minimumLineSpacing = 10
  156. collectionViewLayout.minimumInteritemSpacing = 0
  157. return collectionViewLayout
  158. }()
  159. //加载数据
  160. func reloadData() {
  161. //collectionView重新加载数据
  162. self.collectionView.reloadData()
  163. //更新collectionView的高度约束
  164. let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
  165. collectionView.snp.remakeConstraints { (make) in
  166. make.top.equalTo(cmtTextView.snp_bottom)
  167. make.left.right.equalToSuperview()
  168. make.height.equalTo(contentSize.height)
  169. make.bottom.equalToSuperview()
  170. }
  171. self.collectionView.collectionViewLayout.invalidateLayout()
  172. }
  173. // 评分视图点击
  174. @objc func starViewDidClick(starView: SwiftyStarRatingView) {
  175. let starValue = Int(starView.value)
  176. if let startChangedBlock = self.startChangedBlock {
  177. startChangedBlock(starValue)
  178. }
  179. }
  180. }
  181. extension OrderCommentTableViewCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate {
  182. func numberOfSections(in collectionView: UICollectionView) -> Int {
  183. return 1
  184. }
  185. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  186. if imgCount < maxImageCount {
  187. return imgCount+1
  188. } else {
  189. return maxImageCount
  190. }
  191. }
  192. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  193. if imgCount < maxImageCount && indexPath.row == imgCount {
  194. // 添加图片
  195. let aCell = OrderCommentDefaultCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  196. if imgCount == 0 {
  197. aCell.noteStr = "添加照片"
  198. } else {
  199. aCell.noteStr = "\(imgCount)/\(maxImageCount)"
  200. }
  201. return aCell
  202. } else {
  203. // 展示图片
  204. let pCell = OrderCommentAddImgCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  205. if indexPath.row < imgCount {
  206. pCell.showImage = self.goodsImageArr![indexPath.row]
  207. }
  208. pCell.delPicBlock = {
  209. [weak self] (idxRow) in
  210. if let delPicTransBlock = self?.delPicTransBlock {
  211. delPicTransBlock(idxRow)
  212. }
  213. }
  214. return pCell
  215. }
  216. }
  217. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  218. return CGSize(width:(kScreenWidth-30-5*3)/4, height: (kScreenWidth-30-5*3)/4)
  219. }
  220. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  221. return UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 15)
  222. }
  223. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  224. if imgCount < maxImageCount && indexPath.row == imgCount {
  225. if let choosePicBlock = self.choosePicBlock {
  226. choosePicBlock()
  227. }
  228. }
  229. }
  230. }