ShopViewShopInfoTableViewCell.swift 7.3 KB

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