Browse Source

购物车选中逻辑

Chris 6 years ago
parent
commit
41c99d67cc

+ 8 - 0
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartAccountView.swift

@@ -18,6 +18,14 @@ class ShoppingCartAccountView: BaseView {
     typealias OrderPayBlock = () -> Void
     var orderPayBlock : OrderPayBlock?
     
+    var isAllSelected: Int? {
+        didSet {
+            // 选中状态
+            let selStatus = isAllSelected == 1 ? true : false
+            allSelectBtn.isSelected = selStatus
+        }
+    }
+    
     override func setupViews() {
         self.backgroundColor = kffffffColor
         addSubview(allSelectBtn)

+ 8 - 1
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartList/ShoppingCartListTableViewHeader.swift

@@ -22,6 +22,14 @@ class ShoppingCartListTableViewHeader: BaseView {
         }
     }
     
+    var isSectionSelected: Int? {
+        didSet {
+            // 选中状态
+            let selStatus = isSectionSelected == 1 ? true : false
+            selectedButton.isSelected = selStatus
+        }
+    }
+    
     override var frame: CGRect {
         get {
             return super.frame
@@ -100,7 +108,6 @@ class ShoppingCartListTableViewHeader: BaseView {
         selectedButton.setImage(kImage(name: "common_uncheck_icon"), for: UIControl.State.normal)
         selectedButton.setImage(kImage(name: "common_check_icon"), for: UIControl.State.selected)
         selectedButton.rx.tap.subscribe(onNext: { [weak self] (data) in
-            
             selectedButton.isSelected = !selectedButton.isSelected
             if let secSelectBlock = self?.secSelectBlock {
                 let isSectionSel: Int = selectedButton.isSelected == true ? 1 : 0

+ 67 - 6
RainbowPlanet/RainbowPlanet/Modules/ShoppingCartModule/ShoppingCart/View/ShoppingCartView.swift

@@ -14,6 +14,7 @@ class ShoppingCartView: BaseView {
     var cartListModelArr : Array<CartProductListModel>? {
         didSet {
             tableView.reloadData()
+            self.judgeAllSelectedStatus()
         }
     }
     
@@ -52,10 +53,9 @@ class ShoppingCartView: BaseView {
     lazy var accountView: ShoppingCartAccountView = {
         let accountView = ShoppingCartAccountView()
         
-        accountView.allSelectBlock = {
+        accountView.allSelectBlock = { [weak self]
             (isAllSelected) in
-            
-            print("选中状态 -- \(isAllSelected)")
+            self?.allSelectedAction(isAllSelected)
         }
         
         accountView.orderPayBlock = {
@@ -82,6 +82,7 @@ class ShoppingCartView: BaseView {
 
 }
 
+// MARK: - tableView dataSource && delegate
 extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
     func numberOfSections(in tableView: UITableView) -> Int {
         // 购物车列表 + 超值热卖
@@ -126,12 +127,16 @@ extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
                 let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
                 cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
                 
-                cell.productSelBlock = {
+                cell.productSelBlock = { [weak self]
                     (isProductSel) in
-                    print("选中状态 -- \(isProductSel)")
+                self?.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isProductSel
+                    
+                    self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
+                    self?.judgeAllSelectedStatus()
+                    
                 }
                 
-                cell.changeProductBlock = {
+                cell.changeProductBlock = { [weak self]
                     (productId, type) in
                     SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
                         // 1.更新数据源
@@ -173,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
         }
@@ -202,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
+    }
+    
+}

+ 1 - 1
RainbowPlanet/RainbowPlanet/Service/Model/ProductModel/ProductCartListModel.swift

@@ -39,7 +39,7 @@ class CartProductListModel : NSObject, Mappable{
         return CartProductListModel()
     }
     required init?(map: Map){}
-    private override init(){}
+    override init(){}
     
     func mapping(map: Map)
     {