|
@@ -14,6 +14,7 @@ class ShoppingCartView: BaseView {
|
|
|
var cartListModelArr : Array<CartProductListModel>? {
|
|
|
didSet {
|
|
|
tableView.reloadData()
|
|
|
+ self.judgeAllSelectedStatus()
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -52,6 +53,11 @@ class ShoppingCartView: BaseView {
|
|
|
lazy var accountView: ShoppingCartAccountView = {
|
|
|
let accountView = ShoppingCartAccountView()
|
|
|
|
|
|
+ accountView.allSelectBlock = { [weak self]
|
|
|
+ (isAllSelected) in
|
|
|
+ self?.allSelectedAction(isAllSelected)
|
|
|
+ }
|
|
|
+
|
|
|
accountView.orderPayBlock = {
|
|
|
[weak self] in
|
|
|
if let orderPayTransBlock = self?.orderPayTransBlock {
|
|
@@ -76,6 +82,7 @@ class ShoppingCartView: BaseView {
|
|
|
|
|
|
}
|
|
|
|
|
|
+// MARK: - tableView dataSource && delegate
|
|
|
extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
// 购物车列表 + 超值热卖
|
|
@@ -119,7 +126,17 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
|
|
|
// 购物车列表Item
|
|
|
let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
|
|
|
cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
|
|
|
- cell.changeProductBlock = {
|
|
|
+
|
|
|
+ cell.productSelBlock = { [weak self]
|
|
|
+ (isProductSel) in
|
|
|
+ self?.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isProductSel
|
|
|
+
|
|
|
+ self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
|
|
|
+ self?.judgeAllSelectedStatus()
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ cell.changeProductBlock = { [weak self]
|
|
|
(productId, type) in
|
|
|
SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
|
|
|
// 1.更新数据源
|
|
@@ -161,7 +178,14 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
|
|
|
if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
|
|
|
let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
|
|
|
headerView.shopName = cartListModelArr![section].shopName
|
|
|
+ headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
|
|
|
+ headerView.secSelectBlock = { [weak self]
|
|
|
+ (isSectionSel) in
|
|
|
+ self?.shopSelectedAction(isSectionSel, section: section)
|
|
|
+ self?.judgeAllSelectedStatus()
|
|
|
+ }
|
|
|
return headerView
|
|
|
+
|
|
|
} else {
|
|
|
return nil
|
|
|
}
|
|
@@ -190,3 +214,52 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
|
|
|
return "删除"
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// MARK: - 购物车逻辑处理
|
|
|
+extension ShoppingCartView {
|
|
|
+
|
|
|
+ // 全选点击事件
|
|
|
+ func allSelectedAction(_ isSelected: Int) {
|
|
|
+ for cartProListMdl in cartListModelArr ?? [] {
|
|
|
+ for productMdl in cartProListMdl.productList! {
|
|
|
+ productMdl.isSelect = isSelected
|
|
|
+ }
|
|
|
+ }
|
|
|
+ tableView.reloadData()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 店铺(全选)点击事件
|
|
|
+ func shopSelectedAction(_ isSelected: Int, section: Int) {
|
|
|
+ let cartProListMdl: CartProductListModel = cartListModelArr![section]
|
|
|
+ for productMdl in cartProListMdl.productList! {
|
|
|
+ productMdl.isSelect = isSelected
|
|
|
+ }
|
|
|
+ self.tableView.reloadSections([section], with: UITableView.RowAnimation.none)
|
|
|
+ self.judgeAllSelectedStatus()
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验Section内商品是否全选
|
|
|
+ func judgeSectionSelectedStatus(_ section: Int) -> Int {
|
|
|
+ let cartProListMdl: CartProductListModel = cartListModelArr![section]
|
|
|
+ for productMdl in cartProListMdl.productList! {
|
|
|
+ if productMdl.isSelect == 0 {
|
|
|
+ return 0
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 1
|
|
|
+ }
|
|
|
+
|
|
|
+ // 校验购物车内商品是否全选
|
|
|
+ func judgeAllSelectedStatus() -> Void {
|
|
|
+ let secCount: Int = cartListModelArr?.isEmpty ?? true ? 0 : cartListModelArr!.count
|
|
|
+ for sec in 0..<secCount {
|
|
|
+ let secStatus: Int = self.judgeSectionSelectedStatus(sec)
|
|
|
+ if secStatus == 0 {
|
|
|
+ self.accountView.isAllSelected = 0
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self.accountView.isAllSelected = 1
|
|
|
+ }
|
|
|
+
|
|
|
+}
|