忙里偷閑,把之前寫(xiě)好的商城購(gòu)物車整理下給大家分享〈植罚現(xiàn)在上面有很多的購(gòu)物車,個(gè)人感覺(jué)寫(xiě)的不是很完整纳击,這里補(bǔ)充了一些续扔。先上圖攻臀,說(shuō)下簡(jiǎn)單的邏輯
購(gòu)物車的邏輯基本主要是在選中對(duì)應(yīng)產(chǎn)品,根據(jù)產(chǎn)品數(shù)量和選中的產(chǎn)品去更改對(duì)應(yīng)價(jià)格纱昧。這個(gè)思路理清了就很簡(jiǎn)單了刨啸。在這個(gè)基礎(chǔ)上我的demo仿照京東做了個(gè)編輯。編輯模式下商品的選擇是跟非編輯模式是不干擾的识脆。下面上代碼
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ù)。同學(xué)們接入的時(shí)候替換成自己的就行了
//判斷組的是否選中狀態(tài)是否修改
NSString * cellTagStr = [NSString stringWithFormat:@"%zd",shopCellTag];
NSIndexPath *indexPath = self.dic[cellTagStr];
MKOrderListModel * listModel = (MKOrderListModel*)self.dateArray[indexPath.section];
//0.便利當(dāng)前組cell上選中按鈕的個(gè)數(shù)
NSInteger seletedNum =0;
for (MKGoodsModel* goodsModel in listModel.goods) {
if (goodsModel.isSelected) {
seletedNum += 1;
}
// 1.當(dāng)前組的cell的個(gè)數(shù) 是否等于 勾選的總數(shù)
if (((MKOrderListModel*)self.dateArray[indexPath.section]).goods.count == seletedNum) {
listModel.groupSelected = YES; //cell改變組頭變?yōu)檫x中
//判斷 //cell改變組頭 //組頭改變?nèi)x
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];
}
// 計(jì)算價(jià)格
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]];
這里是主要的邏輯存璃,主要是通過(guò)判斷區(qū)的選中的狀態(tài)或者個(gè)別商品的選中狀態(tài)去改變對(duì)于商品的價(jià)格仑荐。這里就不詳細(xì)說(shuō)了 ,同學(xué)們自己看demo纵东,文章末尾上鏈接粘招。(有疑問(wèn)或者發(fā)現(xiàn)有誤的可以相互交流)
最后面就是編輯狀態(tài):
在進(jìn)入編輯狀態(tài)的時(shí)候取出對(duì)應(yīng)商店里選中的商品(注意:demo中的商品是通過(guò)商品的ID作為key,一定要唯一值不然可能或出現(xiàn)商品錯(cuò)亂)存放是以字典形式存放在數(shù)組中偎球、當(dāng)用戶點(diǎn)擊完成后返回非編輯模式洒扎,在通過(guò)數(shù)組遍歷,根據(jù)字典里面的key判斷商品是否被選中衰絮,選中的改變商品選中狀態(tài)袍冷。
sender.selected = !sender.selected;
if (sender.selected) { // 進(jìn)入編輯狀態(tài) 記錄原數(shù)據(jù) 清空選中狀態(tài)
[_rightEditor setTitle:@"完成" forState:(UIControlStateNormal)];
[self.rowArray removeAllObjects];
// 點(diǎn)擊編輯清除所有狀態(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];
}];
// 隱藏價(jià)格刷新數(shù)據(jù)
[_footView sethiddenViewWithPrice:YES settle:@"刪除"];
[_footView sethiddenViewWithCollectionButton:NO];
[_shopTable reloadData];
}else {
// 清除價(jià)格重新計(jì)算
_totalNum = 0;
[_rightEditor setTitle:@"編輯" forState:(UIControlStateNormal)];
// 重新計(jì)算商品之前先清除所有收藏的選中狀態(tài)
[self changShopGoodblack:^(NSInteger section, NSInteger row) {
MKGoodsModel *goodsModel = ((MKOrderListModel*)self.dateArray[section]).goods[row];
goodsModel.isSelected = NO;
}];
// 回復(fù)購(gòu)物車原來(lái)選中的商品
[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:@"-"];
// 判斷對(duì)應(yīng)的分區(qū)
if ([good_p[0]integerValue] == section)
{
// 判斷對(duì)應(yīng)分區(qū)的商品id(商品在編輯狀態(tài)時(shí)可能已被刪除)
if ([good_p[1] isEqualToString:goodsModel.goods_id])
{
goodsModel.isSelected = YES;
}
}
}
[self shopCellSelectedClicksection:section row:row];
}];
[_footView sethiddenViewWithPrice:NO settle:@"結(jié)算"];
[_footView sethiddenViewWithCollectionButton:YES];
[_shopTable reloadData];
}
當(dāng)然如果你有好的方式也可以跟大家分享下。
根據(jù)我對(duì)京東APP的觀察猫牡,他們的商品數(shù)據(jù)選中狀態(tài)胡诗,數(shù)量改變等都是通過(guò)服務(wù)端存儲(chǔ)的,前端只需要傳值然后更新對(duì)應(yīng)的cell就行了淌友。demo里面選中狀態(tài)那樣寫(xiě)是因?yàn)槲覀冞@邊的服務(wù)端不愿去做這些繁雜的東西煌恢。所以寫(xiě)得并不是很完整,大家可根據(jù)自己需求去小改下震庭。
最后--------------------https://gitee.com/xpyt/goodscart/tree/master