ShoppingCartView.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // ShoppingCartView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/5/8.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 购物车--首页View
  8. import UIKit
  9. class ShoppingCartView: BaseView {
  10. // 购物车列表ModelArr
  11. var cartListModelArr : Array<CartProductListModel>? {
  12. didSet {
  13. tableView.reloadData()
  14. }
  15. }
  16. // 热销ModelArr
  17. var hotSaleModelArr : Array<ProductSearchModel>? {
  18. didSet {
  19. let sectionIdx = cartListModelArr?.count ?? 1
  20. self.tableView.reloadSections([sectionIdx], with: UITableView.RowAnimation.none)
  21. }
  22. }
  23. typealias OrderPayTransBlock = () -> Void
  24. var orderPayTransBlock : OrderPayTransBlock?
  25. override func setupViews() {
  26. self.backgroundColor = kf7f8faColor
  27. addSubview(accountView)
  28. addSubview(tableView)
  29. let emptyView = EmptyView.shared.diyCustomEmptyViewStyle2(iconStr: "page04", titleStr: "当前暂无数据")
  30. emptyView.contentViewY = kScaleValue(value: 182)
  31. tableView.ly_emptyView = emptyView
  32. tableView.ly_startLoading()
  33. }
  34. override func setupLayouts() {
  35. accountView.snp.makeConstraints { (make) in
  36. make.left.right.bottom.equalToSuperview()
  37. make.height.equalTo(48)
  38. }
  39. tableView.snp.makeConstraints { (make) in
  40. make.top.left.right.equalToSuperview()
  41. make.bottom.equalTo(accountView.snp_top).offset(0)
  42. }
  43. }
  44. lazy var accountView: ShoppingCartAccountView = {
  45. let accountView = ShoppingCartAccountView()
  46. accountView.orderPayBlock = {
  47. [weak self] in
  48. if let orderPayTransBlock = self?.orderPayTransBlock {
  49. orderPayTransBlock()
  50. }
  51. }
  52. return accountView
  53. }()
  54. lazy var tableView: UITableView = {
  55. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  56. tableView.separatorStyle = .none
  57. tableView.backgroundColor = kf7f8faColor
  58. tableView.dataSource = self
  59. tableView.delegate = self
  60. tableView.estimatedRowHeight = 0.000001
  61. tableView.estimatedSectionFooterHeight = 0.000001
  62. tableView.estimatedSectionHeaderHeight = 0.000001
  63. return tableView
  64. }()
  65. }
  66. extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
  67. func numberOfSections(in tableView: UITableView) -> Int {
  68. // 购物车列表 + 超值热卖
  69. return cartListModelArr?.isEmpty ?? true ? 2 : cartListModelArr!.count + 1
  70. }
  71. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  72. if cartListModelArr?.isEmpty ?? true {
  73. return 1
  74. } else {
  75. if section < cartListModelArr!.count {
  76. return cartListModelArr![section].productList?.isEmpty ?? true ? 0 : cartListModelArr![section].productList!.count
  77. } else {
  78. return 1
  79. }
  80. }
  81. }
  82. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  83. if cartListModelArr?.count == 0 {
  84. switch indexPath.section {
  85. case 0:
  86. // 购物车列表为空
  87. let cell = ShoppingCartListNoneItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  88. return cell
  89. case 1:
  90. // 超值热卖
  91. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  92. hotSaleCell.frame = tableView.bounds
  93. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  94. hotSaleCell.layoutIfNeeded()
  95. hotSaleCell.reloadData()
  96. return hotSaleCell
  97. default:
  98. return UITableViewCell()
  99. }
  100. } else {
  101. if indexPath.section < cartListModelArr!.count {
  102. // 购物车列表Item
  103. let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  104. cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
  105. cell.changeProductBlock = {
  106. (productId, type) in
  107. SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
  108. // 1.更新数据源
  109. let cartAmountMdl = amountData as? CartAmountModel
  110. let amount = cartAmountMdl?.amount
  111. self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount = amount
  112. // 2.刷新cell
  113. let indexPath = IndexPath(item: indexPath.row, section: indexPath.section)
  114. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  115. })
  116. }
  117. return cell
  118. } else {
  119. // 超值热卖
  120. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  121. hotSaleCell.frame = tableView.bounds
  122. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  123. hotSaleCell.layoutIfNeeded()
  124. hotSaleCell.reloadData()
  125. return hotSaleCell
  126. }
  127. }
  128. }
  129. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  130. return UITableView.automaticDimension
  131. }
  132. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  133. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  134. return 58
  135. } else {
  136. return 10
  137. }
  138. }
  139. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  140. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  141. let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
  142. headerView.shopName = cartListModelArr![section].shopName
  143. return headerView
  144. } else {
  145. return nil
  146. }
  147. }
  148. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  149. return 0.000001
  150. }
  151. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  152. return nil
  153. }
  154. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  155. if editingStyle == .delete {
  156. // 删除商品
  157. let productId = cartListModelArr![indexPath.section].productList?[indexPath.row].id
  158. SwiftMoyaNetWorkServiceProduct.shared().productCartDeleteApi(id: productId ?? 0) { [weak self] (data) -> (Void) in
  159. self?.cartListModelArr![indexPath.section].productList?.remove(at: indexPath.row)
  160. tableView.deleteRows(at: [indexPath], with: .none)
  161. }
  162. }
  163. }
  164. func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  165. return "删除"
  166. }
  167. }