ShopView.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // ShopView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/5/12.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class ShopView: BaseView {
  10. typealias DidSelectItemBlock = (_ indexPath: IndexPath) -> Void
  11. var didSelectItemBlock : DidSelectItemBlock?
  12. typealias PlusCloSure = () -> Void
  13. var plusCloSure : PlusCloSure?
  14. var shopModel : ShopModel? {
  15. didSet {
  16. tableView.reloadData()
  17. }
  18. }
  19. var paginationModel: PaginationModel? {
  20. didSet {
  21. tableView.reloadData()
  22. }
  23. }
  24. var productSearchModelArray : Array<ProductSearchModel>? {
  25. didSet {
  26. if productSearchModelArray?.isEmpty ?? true {
  27. tableView.isHiddenFooter(true)
  28. }else {
  29. tableView.isHiddenFooter(false)
  30. }
  31. tableView.reloadData()
  32. }
  33. }
  34. var shopSectionHeaderModel : ShopSectionHeaderModel? {
  35. didSet {
  36. tableView.reloadData()
  37. }
  38. }
  39. override func setupViews() {
  40. addSubview(tableView)
  41. }
  42. override func setupLayouts() {
  43. tableView.snp.makeConstraints { (make) in
  44. make.edges.equalToSuperview()
  45. }
  46. }
  47. lazy var tableView: UITableView = {
  48. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.plain)
  49. tableView.separatorStyle = .none
  50. tableView.backgroundColor = kf7f8faColor
  51. tableView.dataSource = self
  52. tableView.delegate = self
  53. tableView.estimatedRowHeight = 82
  54. tableView.estimatedSectionFooterHeight = 0.000001
  55. tableView.estimatedSectionHeaderHeight = 0.000001
  56. return tableView
  57. }()
  58. }
  59. extension ShopView : UITableViewDelegate, UITableViewDataSource {
  60. func numberOfSections(in tableView: UITableView) -> Int {
  61. return 2
  62. }
  63. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  64. return 1
  65. }
  66. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  67. switch indexPath.section {
  68. case 0:
  69. let cell = ShopViewShopInfoTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  70. cell.shopModel = shopModel
  71. cell.paginationModel = paginationModel
  72. cell.lookButtonClosure = {
  73. [weak self] isSelected in
  74. self?.shopModel?.isOpen = isSelected
  75. tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  76. }
  77. return cell
  78. case 1:
  79. let cell = ShopViewTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  80. cell.productSearchModelArray = productSearchModelArray
  81. cell.frame = tableView.bounds
  82. cell.layoutIfNeeded()
  83. cell.reloadData()
  84. cell.didSelectItemBlock = {
  85. [weak self] indexPath in
  86. if let didSelectItemBlock = self?.didSelectItemBlock {
  87. didSelectItemBlock(indexPath)
  88. }
  89. }
  90. cell.plusCloSure = {
  91. if let plusCloSure = self.plusCloSure {
  92. plusCloSure()
  93. }
  94. }
  95. return cell
  96. default:
  97. return UITableViewCell()
  98. }
  99. }
  100. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  101. return UITableView.automaticDimension
  102. }
  103. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  104. switch section {
  105. case 0:
  106. return 0.000001
  107. case 1:
  108. return 44
  109. default:
  110. return 0
  111. }
  112. }
  113. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  114. switch section {
  115. case 0:
  116. return nil
  117. case 1:
  118. let view = ShopSectionHeaderView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 44))
  119. view.shopSectionHeaderModel = self.shopSectionHeaderModel
  120. return view
  121. default:
  122. return nil
  123. }
  124. }
  125. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  126. return 0.000001
  127. }
  128. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  129. return nil
  130. }
  131. }