仿京東購物車iOS

忙里偷閑,把之前寫好的商城購物車整理下給大家分享。現(xiàn)在上面有很多的購物車,個人感覺寫的不是很完整,這里補充了一些。先上圖,說下簡單的邏輯

示意圖.png

購物車的邏輯基本主要是在選中對應產品,根據(jù)產品數(shù)量和選中的產品去更改對應價格。這個思路理清了就很簡單了。在這個基礎上我的demo仿照京東做了個編輯。編輯模式下商品的選擇是跟非編輯模式是不干擾的。下面上代碼

NSString*pathStr = [[NSBundle mainBundle] pathForResource:@"YSDota" ofType:nil];
    NSData* data = [NSData dataWithContentsOfFile:pathStr];
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    NSArray * array = json[@"group_goods_cart"];
    for (NSDictionary* list in array) {
        MKOrderListModel* obj = [MKOrderListModel mj_objectWithKeyValues:list];
        [self.dateArray addObject:obj];
        
    }

先獲取數(shù)據(jù),上面代碼是本地的數(shù)據(jù)。同學們接入的時候替換成自己的就行了

//判斷組的是否選中狀態(tài)是否修改
    NSString * cellTagStr = [NSString stringWithFormat:@"%zd",shopCellTag];
    NSIndexPath *indexPath = self.dic[cellTagStr];
    
    MKOrderListModel * listModel = (MKOrderListModel*)self.dateArray[indexPath.section];
    //0.便利當前組cell上選中按鈕的個數(shù)
    NSInteger seletedNum =0;
    for (MKGoodsModel* goodsModel in listModel.goods) {
        if (goodsModel.isSelected) {
            seletedNum += 1;
        }
        // 1.當前組的cell的個數(shù) 是否等于 勾選的總數(shù)
        if (((MKOrderListModel*)self.dateArray[indexPath.section]).goods.count == seletedNum) {
            listModel.groupSelected = YES; //cell改變組頭變?yōu)檫x中
            //判斷  //cell改變組頭 //組頭改變全選
            NSInteger selectedNum = 0 ;
            for (MKOrderListModel * tempListModel in self.dateArray) {//遍歷所有組
                if (tempListModel.groupSelected) {//如果組頭是選中的
                    selectedNum += 1;
                }
                if (selectedNum == self.dateArray.count) {
                    [_footView setDotType:YES];
                }
            }
        } else {
            listModel.groupSelected = NO;
            [_footView setDotType:NO];
        }
        [_shopTable reloadData];
    }
    
    // 計算價格
    MKGoodsModel *goodsModel = ((MKOrderListModel*)self.dateArray[indexPath.section]).goods[indexPath.row];
    float goods_price = goodsModel.goods_price;
    float goods_number = goodsModel.goods_number;
    if (!goodsModel.isSelected) {
        
        _totalNum -= goods_price*goods_number;
        
    }else {
        
        _totalNum+= goods_price*goods_number;
        
    }
    [_footView setSettlementPrice:[NSString stringWithFormat:@"¥%.2f",_totalNum -1 + 1]];

這里是主要的邏輯,主要是通過判斷區(qū)的選中的狀態(tài)或者個別商品的選中狀態(tài)去改變對于商品的價格。這里就不詳細說了 ,同學們自己看demo,文章末尾上鏈接。(有疑問或者發(fā)現(xiàn)有誤的可以相互交流)

最后面就是編輯狀態(tài):
在進入編輯狀態(tài)的時候取出對應商店里選中的商品(注意:demo中的商品是通過商品的ID作為key,一定要唯一值不然可能或出現(xiàn)商品錯亂)存放是以字典形式存放在數(shù)組中、當用戶點擊完成后返回非編輯模式,在通過數(shù)組遍歷,根據(jù)字典里面的key判斷商品是否被選中,選中的改變商品選中狀態(tài)。

sender.selected = !sender.selected;
    if (sender.selected) { // 進入編輯狀態(tài) 記錄原數(shù)據(jù) 清空選中狀態(tài)
        
        [_rightEditor setTitle:@"完成" forState:(UIControlStateNormal)];
        [self.rowArray removeAllObjects];
        // 點擊編輯清除所有狀態(tài)
        [self changShopGoodblack:^(NSInteger section, NSInteger row) {
            
            MKGoodsModel *goodsModel = ((MKOrderListModel*)self.dateArray[section]).goods[row];
            
            if (goodsModel.isSelected) { //   記錄選中的商品 (選唯一值作為判斷 一般商品id都是唯一的)
                
                if (![self.rowArray containsObject:goodsModel.goods_id]) {
                    [self.rowArray addObject:[NSString stringWithFormat:@"%ld-%@",(long)section,goodsModel.goods_id]];
                }
                
            }
            goodsModel.isSelected = NO;
            [self shopCellSelectedClicksection:section row:row];
        }];
        // 隱藏價格刷新數(shù)據(jù)
        [_footView sethiddenViewWithPrice:YES settle:@"刪除"];
        [_footView sethiddenViewWithCollectionButton:NO];
        [_shopTable reloadData];
        
    }else {
        
        // 清除價格重新計算
        _totalNum = 0;
        [_rightEditor setTitle:@"編輯" forState:(UIControlStateNormal)];
        // 重新計算商品之前先清除所有收藏的選中狀態(tài)
        [self changShopGoodblack:^(NSInteger section, NSInteger row) {
            
            MKGoodsModel *goodsModel = ((MKOrderListModel*)self.dateArray[section]).goods[row];
            goodsModel.isSelected = NO;

        }];
        // 回復購物車原來選中的商品
        [self changShopGoodblack:^(NSInteger section, NSInteger row) {
            
            MKGoodsModel *goodsModel = ((MKOrderListModel*)self.dateArray[section]).goods[row];
            for (NSString *selectGood in self.rowArray)
            {
                NSArray *good_p = [selectGood componentsSeparatedByString:@"-"];
                // 判斷對應的分區(qū)
                if ([good_p[0]integerValue] == section)
                {
                    // 判斷對應分區(qū)的商品id(商品在編輯狀態(tài)時可能已被刪除)
                    if ([good_p[1] isEqualToString:goodsModel.goods_id])
                    {
                        goodsModel.isSelected = YES;
                    }
                }
            }
            [self shopCellSelectedClicksection:section row:row];
        }];
        [_footView sethiddenViewWithPrice:NO settle:@"結算"];
        [_footView sethiddenViewWithCollectionButton:YES];
        [_shopTable reloadData];
    }

當然如果你有好的方式也可以跟大家分享下。
根據(jù)我對京東APP的觀察,他們的商品數(shù)據(jù)選中狀態(tài),數(shù)量改變等都是通過服務端存儲的,前端只需要傳值然后更新對應的cell就行了。demo里面選中狀態(tài)那樣寫是因為我們這邊的服務端不愿去做這些繁雜的東西。所以寫得并不是很完整,大家可根據(jù)自己需求去小改下。
最后--------------------https://gitee.com/xpyt/goodscart/tree/master

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容