YCOrderShopCartCell.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // YCOrderShopCartCell.swift
  3. // learnSwift
  4. //
  5. // Created by zyc on 2019/12/23.
  6. // Copyright © 2019 张言超. All rights reserved.
  7. //
  8. import UIKit
  9. @objc protocol YCOrderShopCartCellDelegate {
  10. /**cell点击了*/
  11. func shopCellDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell)
  12. /**加号按钮点击了*/
  13. func addBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell)
  14. /**减号按钮点击了*/
  15. func minusBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell)
  16. /**左侧选择按钮点击了*/
  17. func leftSelBtnDidPress(goodModel: YCOrderGoodsModel, cell: YCOrderShopCartCell, isSel: Bool)
  18. }
  19. class YCOrderShopCartCell: UITableViewCell {
  20. let cellHeight: CGFloat = 100//整体的高度
  21. let boardWidth: CGFloat = 10
  22. let backHeight: CGFloat = 90//背景内容的高度
  23. let backWidth: CGFloat = kScreenWidth-10 //内容的宽
  24. let smallBtnWH: CGFloat = 25 //右下角按钮尺寸
  25. let leftSelBtnWH: CGFloat = 30
  26. /** 背景View */
  27. var backView: UIView?
  28. /** 左侧选择的按钮 */
  29. var leftSelBtn: UIButton?
  30. /** 商品的图片 */
  31. var commodityImageView: UIImageView?
  32. /** 商品名字 */
  33. var commodityNameLabel: UILabel?
  34. /** 商品详情label */
  35. var commodityDetailLabel: UILabel?
  36. /** 价格label */
  37. var priceLabel: UILabel?
  38. /** 减号按钮 */
  39. var minusBtn: UIButton?
  40. /** 数量label */
  41. var countLabel: UILabel?
  42. /** 加号按钮 */
  43. var addBtn: UIButton?
  44. /** 左侧按钮 */
  45. var isLeftBtnSel: Bool = false
  46. /** 删除按钮 */
  47. var delectBtn: UIButton?
  48. //代理
  49. weak var YCcellDelegate: YCOrderShopCartCellDelegate?
  50. //数据模型
  51. private var _xgoodModel: YCOrderGoodsModel?
  52. var xgoodModel: YCOrderGoodsModel? {
  53. get {
  54. return _xgoodModel
  55. }
  56. set {
  57. _xgoodModel = newValue
  58. if _xgoodModel!.goodsIsSelect {
  59. self .setBtnImage(imageName: "sight_moments_mute_selected", button: self.leftSelBtn!)
  60. self.isLeftBtnSel = true
  61. } else {
  62. self .setBtnImage(imageName: "sight_moments_mute_select", button: self.leftSelBtn!)
  63. self.isLeftBtnSel = false
  64. }
  65. self.commodityNameLabel?.text = _xgoodModel?.goodsName
  66. self.countLabel?.text = "\(_xgoodModel?.goodsCount ?? 0)"
  67. self.priceLabel?.text = _xgoodModel?.goodsPrice
  68. self.commodityDetailLabel?.text = _xgoodModel?.goodsDescription
  69. self.commodityImageView?.loadImage(imgUrl: (_xgoodModel?.goodsIconUrl)!, defaultImage: "")
  70. }
  71. }
  72. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  73. super.init(style: style, reuseIdentifier: reuseIdentifier)
  74. //背景
  75. self.backView = UIView.init(frame: CGRect(x: 5, y: boardWidth*0.5, width: kScreenWidth - boardWidth, height: cellHeight-boardWidth))
  76. self.backView?.backgroundColor = .white
  77. self.contentView.addSubview(self.backView!)
  78. //左侧选中按钮
  79. self.leftSelBtn = UIButton.init()
  80. self.leftSelBtn?.addTarget(self, action: #selector(leftSelBtnDidPress(button:)), for: .touchUpInside)
  81. self.backView?.addSubview(self.leftSelBtn!)
  82. //商品imageview
  83. self.commodityImageView = UIImageView.init()
  84. self.commodityImageView?.image = UIImage.init(named: "CreditCard_ShoppingBag")
  85. self.backView?.addSubview(self.commodityImageView!)
  86. //商品标题label
  87. self.commodityNameLabel = UILabel.init()
  88. self.commodityNameLabel?.textColor = .black
  89. self.commodityNameLabel?.font = UIFont.systemFont(ofSize: 18)
  90. self.backView?.addSubview(self.commodityNameLabel!)
  91. //商品详情描述label
  92. self.commodityDetailLabel = UILabel.init()
  93. self.commodityDetailLabel?.font = UIFont.systemFont(ofSize: 15)
  94. self.commodityDetailLabel?.textColor = UIColor.colorWithCustom(r: 245, g: 61, b: 28)
  95. self.commodityDetailLabel?.numberOfLines = 0
  96. self.backView?.addSubview(self.commodityDetailLabel!)
  97. //价格label
  98. self.priceLabel = UILabel.init()
  99. self.priceLabel?.textColor = UIColor.colorWithCustom(r: 245, g: 61, b: 28)
  100. self.priceLabel?.textColor = UIColor.clear
  101. self.priceLabel?.font = UIFont.systemFont(ofSize: 18)
  102. self.priceLabel?.textAlignment = .right
  103. self.backView?.addSubview(self.priceLabel!)
  104. //删除按钮
  105. self.delectBtn = UIButton.init()
  106. self.delectBtn?.setImage(UIImage.init(named: "shanchu"), for: .normal)
  107. self.backView?.addSubview(self.delectBtn!)
  108. //减号按钮
  109. self.minusBtn = UIButton.init()
  110. self.minusBtn?.setImage(UIImage.init(named: "RemoveGroupMemberBtn"), for: .normal)
  111. self.minusBtn?.setImage(UIImage.init(named: "RemoveGroupMemberBtnHL"), for: .highlighted)
  112. self.minusBtn?.addTarget(self, action: #selector(minusBtnDidPress(button:)), for: .touchUpInside)
  113. self.backView?.addSubview(self.minusBtn!)
  114. //中间数量label
  115. self.countLabel = UILabel.init()
  116. self.countLabel?.textAlignment = .center
  117. self.countLabel?.textColor = .black
  118. self.countLabel?.font = UIFont.systemFont(ofSize: 18)
  119. self.backView?.addSubview(self.countLabel!)
  120. //加号按钮
  121. self.addBtn = UIButton.init()
  122. self.addBtn?.setImage(UIImage.init(named: "AlbumAddBtn"), for: .normal)
  123. self.addBtn?.setImage(UIImage.init(named: "AlbumAddBtnHL"), for: .highlighted)
  124. self.addBtn?.addTarget(self, action: #selector(addBtnDidPress(button:)), for: .touchUpInside)
  125. self.backView?.addSubview(self.addBtn!)
  126. //点击手势
  127. let sigleTap = UITapGestureRecognizer.init(target: self, action: #selector(cellDidPress(tap:)))
  128. self.addGestureRecognizer(sigleTap)
  129. self.setNeedsDisplay()
  130. }
  131. required init?(coder: NSCoder) {
  132. fatalError("init(coder:) has not been implemented")
  133. }
  134. override func awakeFromNib() {
  135. super.awakeFromNib()
  136. }
  137. override func setSelected(_ selected: Bool, animated: Bool) {
  138. super.setSelected(selected, animated: animated)
  139. // Configure the view for the selected state
  140. }
  141. override func layoutSubviews() {
  142. super.layoutSubviews()
  143. self.leftSelBtn?.frame = CGRect(x: boardWidth, y: (backHeight - leftSelBtnWH)*0.5, width: leftSelBtnWH, height: leftSelBtnWH)
  144. self.commodityImageView?.frame = CGRect(x: 2*boardWidth+leftSelBtnWH, y: boardWidth*0.5, width: backHeight - boardWidth, height: backHeight - boardWidth)
  145. self.priceLabel?.frame = CGRect(x: backWidth - 80 - boardWidth, y: boardWidth, width: 80, height: 30)
  146. self.delectBtn?.frame = CGRect(x: backWidth - 20 - boardWidth, y: boardWidth, width: 30, height: 30)
  147. self.commodityNameLabel?.frame = CGRect(x: (self.commodityImageView?.yc_right())! + boardWidth, y: boardWidth, width: backWidth - 5*boardWidth - leftSelBtnWH - self.commodityImageView!.yc_width() - self.delectBtn!.yc_width(), height: 25)
  148. self.addBtn?.frame = CGRect(x: backWidth - boardWidth - smallBtnWH, y: (self.backView?.yc_bottom())! - boardWidth - smallBtnWH, width: smallBtnWH, height: smallBtnWH)
  149. self.countLabel?.frame = CGRect(x: (self.addBtn?.yc_originX())! - boardWidth*0.5-smallBtnWH, y: (self.addBtn?.yc_originY())!, width: smallBtnWH, height: smallBtnWH)
  150. self.minusBtn?.frame = CGRect(x: (self.countLabel?.yc_originX())! - boardWidth*0.5 - smallBtnWH, y: (self.addBtn?.yc_originY())!, width: smallBtnWH, height: smallBtnWH)
  151. self.commodityDetailLabel?.frame = CGRect(x: (self.commodityImageView?.yc_right())! + boardWidth, y: (self.commodityNameLabel?.yc_bottom())! + boardWidth*0.5, width: backWidth - 5.5 * boardWidth - (self.leftSelBtn?.yc_width())! - (self.commodityImageView!.yc_width()) - smallBtnWH*3, height: backHeight - boardWidth * 2 - (self.commodityNameLabel?.yc_height())!)
  152. }
  153. // MARK: - - activity
  154. /** 左侧选择的按钮点击了 */
  155. @objc func leftSelBtnDidPress(button: UIButton) {
  156. if self.isLeftBtnSel {
  157. self.setBtnImage(imageName: "sight_moments_mute_select", button: self.leftSelBtn!)
  158. self.isLeftBtnSel = false
  159. } else {
  160. self.setBtnImage(imageName: "sight_moments_mute_selected", button: self.leftSelBtn!)
  161. self.isLeftBtnSel = true
  162. }
  163. if self.YCcellDelegate != nil {
  164. self.YCcellDelegate?.leftSelBtnDidPress(goodModel: self.xgoodModel!, cell: self, isSel: self.isLeftBtnSel)
  165. }
  166. }
  167. /** 减号按钮点击了 */
  168. @objc func minusBtnDidPress(button: UIButton) {
  169. if self.YCcellDelegate != nil {
  170. self.YCcellDelegate?.minusBtnDidPress(goodModel: self.xgoodModel!, cell: self)
  171. }
  172. }
  173. /** 加号按钮点击了 */
  174. @objc func addBtnDidPress(button: UIButton) {
  175. if self.YCcellDelegate != nil {
  176. self.YCcellDelegate?.addBtnDidPress(goodModel: self.xgoodModel!, cell: self)
  177. }
  178. }
  179. /** cell点击 */
  180. @objc func cellDidPress(tap: UITapGestureRecognizer) {
  181. if self.YCcellDelegate != nil {
  182. self.YCcellDelegate?.shopCellDidPress(goodModel: self.xgoodModel!, cell: self)
  183. }
  184. }
  185. // MARK: - - tool_func
  186. func setBtnImage(imageName: String, button: UIButton) {
  187. button.setImage(UIImage.init(named: imageName), for: .normal)
  188. }
  189. }