ShopViewShopInfoTableViewCell.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // ShopViewShopInfoTableViewCell.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/5/12.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. import RxSwift
  10. class ShopViewShopInfoTableViewCell: UITableViewCell {
  11. let disposeBag = DisposeBag()
  12. typealias LookButtonClosure = (_ isSelected: Bool) -> Void
  13. var lookButtonClosure : LookButtonClosure?
  14. class func cellWith(tableView:UITableView,indexPath:IndexPath) -> ShopViewShopInfoTableViewCell {
  15. let ID = "ShopViewShopInfoTableViewCell"
  16. tableView.register(ShopViewShopInfoTableViewCell.self, forCellReuseIdentifier: ID)
  17. let cell : ShopViewShopInfoTableViewCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! ShopViewShopInfoTableViewCell
  18. cell.indexPath = indexPath
  19. return cell
  20. }
  21. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  22. super.init(style: style, reuseIdentifier: reuseIdentifier)
  23. setupViews()
  24. setupLayouts()
  25. }
  26. required init?(coder aDecoder: NSCoder) {
  27. fatalError("init(coder:) has not been implemented")
  28. }
  29. var indexPath: IndexPath? {
  30. didSet {
  31. }
  32. }
  33. //MRAK: - 设置View
  34. func setupViews() {
  35. self.selectionStyle = .none
  36. backgroundColor = kFFA42FColor
  37. addSubview(shopIconImageView)
  38. addSubview(shopNameLabel)
  39. addSubview(productNumberLabel)
  40. addSubview(descriptionLabel)
  41. addSubview(lookButton)
  42. }
  43. func setupLayouts() {
  44. shopIconImageView.snp.makeConstraints { (make) in
  45. make.top.equalTo(12)
  46. make.left.equalTo(14)
  47. make.size.equalTo(60)
  48. }
  49. shopNameLabel.snp.makeConstraints { (make) in
  50. make.left.equalTo(shopIconImageView.snp.right).offset(10)
  51. make.right.equalTo(-14)
  52. make.height.equalTo(14)
  53. make.top.equalTo(shopIconImageView).offset(11)
  54. }
  55. productNumberLabel.snp.makeConstraints { (make) in
  56. make.left.equalTo(shopIconImageView.snp.right).offset(10)
  57. make.right.equalTo(-14)
  58. make.height.equalTo(14)
  59. make.bottom.equalTo(shopIconImageView).offset(-11)
  60. }
  61. descriptionLabel.snp.remakeConstraints { (make) in
  62. make.top.equalTo(shopIconImageView.snp.bottom).offset(10)
  63. make.left.equalTo(14)
  64. make.right.equalTo(-14)
  65. make.bottom.equalTo(-35)
  66. }
  67. lookButton.snp.makeConstraints { (make) in
  68. make.top.equalTo(descriptionLabel.snp.bottom).offset(10)
  69. make.right.equalTo(-18)
  70. make.bottom.equalTo(-10)
  71. }
  72. }
  73. lazy var shopIconImageView: UIImageView = {
  74. let shopIconImageView = UIImageView()
  75. shopIconImageView.image = kImage(name: "pic_preload")
  76. return shopIconImageView
  77. }()
  78. lazy var shopNameLabel: UILabel = {
  79. let shopNameLabel = UILabel()
  80. shopNameLabel.text = "彩虹星球专营店"
  81. shopNameLabel.textColor = UIColor.white
  82. shopNameLabel.font = kMediumFont15
  83. return shopNameLabel
  84. }()
  85. lazy var productNumberLabel: UILabel = {
  86. let productNumberLabel = UILabel()
  87. productNumberLabel.text = "全部商品:200"
  88. productNumberLabel.textColor = UIColor.white
  89. productNumberLabel.font = kRegularFont14
  90. return productNumberLabel
  91. }()
  92. lazy var descriptionLabel: UILabel = {
  93. let descriptionLabel = UILabel()
  94. descriptionLabel.textColor = UIColor.white
  95. descriptionLabel.font = kRegularFont14
  96. descriptionLabel.numberOfLines = 0
  97. descriptionLabel.sizeToFit()
  98. return descriptionLabel
  99. }()
  100. lazy var lookButton: UIButton = {
  101. let lookButton = UIButton(type: UIButton.ButtonType.custom)
  102. lookButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
  103. lookButton.titleLabel?.font = kRegularFont14
  104. lookButton.isHidden = true
  105. lookButton.rx.tap.subscribe(onNext: { [weak self] (data) in
  106. lookButton.isSelected = !lookButton.isSelected
  107. if let lookButtonClosure = self?.lookButtonClosure {
  108. lookButtonClosure(lookButton.isSelected)
  109. }
  110. }).disposed(by: disposeBag)
  111. return lookButton
  112. }()
  113. var shopModel : ShopModel? {
  114. didSet {
  115. let str = shopModel?.shopDesc
  116. let attributeString = NSMutableAttributedString(string:str ?? "")
  117. attributeString.changeAllLineSpacing(2)
  118. descriptionLabel.attributedText = attributeString
  119. let dict = [
  120. NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14),
  121. ]
  122. let height: CGFloat = str?.boundingRect(with: CGSize(width: kScreenWidth-60-28-10, height:CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: dict, context: nil).size.height ?? 0
  123. let labelHeight = height
  124. let count = Int((labelHeight) / descriptionLabel.font.lineHeight)
  125. if (shopModel?.isOpen ?? false) {
  126. descriptionLabel.numberOfLines = 0
  127. descriptionLabel.lineBreakMode = .byCharWrapping
  128. lookButton.isSelected = true
  129. lookButton.setImage(UIImage.init(named: "common_word_packup"), for: UIControl.State.selected)
  130. lookButton.setTitle("收起", for: UIControl.State.normal)
  131. } else {
  132. descriptionLabel.numberOfLines = 2
  133. descriptionLabel.lineBreakMode = .byTruncatingTail
  134. lookButton.isSelected = false
  135. lookButton.setImage(kImage(name: "common_word_unfold"), for: UIControl.State.normal)
  136. lookButton.setTitle("查看更多", for: UIControl.State.normal)
  137. }
  138. lookButton.layoutButton(edgeInsetsStyle: ButtonEdgeInsetsStyle.right, imageTitleSpace: 4)
  139. shopIconImageView.kf.setImage(with: kURLImage(name: shopModel?.logoImg ?? "pic_preload"), placeholder: kImage(name: "pic_preload"))
  140. shopNameLabel.text = shopModel?.shopName
  141. if count > 2 {
  142. descriptionLabel.snp.remakeConstraints { (make) in
  143. make.top.equalTo(shopIconImageView.snp.bottom).offset(10)
  144. make.left.equalTo(14)
  145. make.right.equalTo(-14)
  146. make.bottom.equalTo(-35)
  147. }
  148. lookButton.isHidden = false
  149. }else {
  150. descriptionLabel.snp.remakeConstraints { (make) in
  151. make.top.equalTo(shopIconImageView.snp.bottom).offset(10)
  152. make.left.equalTo(14)
  153. make.right.equalTo(-14)
  154. make.bottom.equalTo(-10)
  155. }
  156. lookButton.isHidden = true
  157. }
  158. }
  159. }
  160. var paginationModel: PaginationModel? {
  161. didSet{
  162. productNumberLabel.text = "全部商品:\(paginationModel?.total ?? 0)"
  163. }
  164. }
  165. }