购物车

作者: small_Sea | 来源:发表于2016-11-22 15:16 被阅读0次
shop.png
<h5>
本人刚开始写简书不久有很多不足之道请包涵
</h5>
<h5>
购物车
</h5>
参考源码
<h5>
特别想把所有逻辑都写在模型中但是还是少不了与VC的交互,购物车无非 就是单选,组选,全选 三个状态的相互关联,我在每个组的模型中与每个item的模型中添加一个BOOL值用来记录按钮的选中状态. 代码如下
</h5>
<pre>

import <Foundation/Foundation.h>

@class shopJsonModel,shopJsonListModel;
@interface shopBaseModel : NSObject
@property (nonatomic,copy) NSString * message;
@property (nonatomic,strong) NSArray * json;
@end

@interface shopJsonModel : NSObject
@property (nonatomic,copy) NSString * shop_name;
@property (nonatomic,strong) NSArray * list;
// 记录组头按钮是否被选择的状态
@property (nonatomic,assign) BOOL isGroupSelect;
@end
@interface shopJsonListModel : NSObject
@property (nonatomic,copy) NSString * goods_name;
@property (nonatomic,copy) NSString * goods_imgurl;
@property (nonatomic,copy) NSString * cash;
// 每个Item上的按钮是否被选择
@property (nonatomic,assign) BOOL isItemSelect;
@end
</pre>

<h6>
下面是几种状态的代理方法,暂时没想到如何在模型中去写这些逻辑
</h6>
<pre>

pragma mark - 全选

  • (void)allButtonSelect:(shopButton *)allButton{
    [self didAllSelect:allButton.selected];
    [self judgeShopMoney];
    }
  • (void)didAllSelect:(BOOL)isAll{
    for (int i = 0 ; i < self.shopModel.json.count; i++) {
    shopJsonModel *json = self.shopModel.json[i];
    json.isGroupSelect = isAll;
    for (int y = 0; y < json.list.count; y++) {
    shopJsonListModel *itemModel = json.list[y];
    itemModel.isItemSelect = isAll;
    }
    }
    [self.tbView reloadData];
    }

pragma mark - 组选

/**
组选
@param group 每一组的按钮
*/

  • (void)didSelectgroup:(shopButton *)group andSection:(NSInteger)section{
    [self setSelectGroup:group.selected andSection:section];
    [self isAllButtonSelect];
    [self judgeShopMoney];
    }
  • (void)setSelectGroup:(BOOL)isGroup andSection:(NSInteger)section{
    shopJsonModel *jsonModel = self.shopModel.json[section];
    for (shopJsonListModel *listModel in jsonModel.list) {
    listModel.isItemSelect = isGroup;
    }
    [self.tbView reloadData];
    }

pragma mark - 单选

/**
选择单独一个商品
@param shop 每一个商品的按钮
*/

  • (void)singleButton:(shopButton *)shop andRowAtIndexPath:(NSIndexPath *)indexPath{
    BOOL isGroup = YES;
    shopJsonModel *jsonModel = self.shopModel.json[indexPath.section];
    for (shopJsonListModel *itemModel in jsonModel.list) {
    if (!itemModel.isItemSelect) {
    isGroup = NO;
    }
    }
    jsonModel.isGroupSelect = isGroup;
    [self isAllButtonSelect];
    [self.tbView reloadData];
    [self judgeShopMoney];
    }
    /**
    判断所有的组头是否被选择
    */
  • (void)isAllButtonSelect{
    BOOL isAll = YES;
    for (int i = 0; i < self.shopModel.json.count; i++) {
    shopJsonModel *m = self.shopModel.json[i];
    if (!m.isGroupSelect) {
    isAll = NO;
    }
    }
    self.allBgView.isAllSelect = isAll;
    }
    /**
    计算商品的价格
    */
    -(void)judgeShopMoney{
    double allPrice = 0;
    for (shopJsonModel *json in self.shopModel.json) {
    for (shopJsonListModel *item in json.list) {
    if (item.isItemSelect) {
    allPrice += [item.cash doubleValue];
    }
    }
    }
    self.allBgView.price=[NSString stringWithFormat:@"%.02f",allPrice];
    NSLog(@"%f",allPrice);
    }
    </pre>

<h5>
如果想要获取哪一个被选中哪一个没有被选中 遍历一下self.shopModel.json 数组.计算商品价格等. 代码如下
</h5>
<pre>
/**
计算商品的价格
*/
-(void)judgeShopMoney{
double allPrice = 0;
for (shopJsonModel *json in self.shopModel.json) {
for (shopJsonListModel *item in json.list) {
if (item.isItemSelect) {
allPrice += [item.cash doubleValue];
}
}
}
self.allBgView.price=[NSString stringWithFormat:@"%.02f",allPrice];
NSLog(@"%f",allPrice);
}
</pre>

相关文章

  • 商城之购物车

    购物车管理: 包含功能:提交商品到购物车、显示购物车列表、删除购物车里商品、修改购物车、清空购物车等等 1、购...

  • 购物车模块实现

    1、购物车列表功能实现 点击加入购物车或者点击购物车图标后进入购物车页面,在购物车页面中首先渲染cartList的...

  • SSM框架学习日记(6)——购物车模块

    购物车相关接口 添加购物车,购物车列表,更新商品数量,删除购物车先新建CartController和CartSer...

  • 8.5-高并发下的互联网电商购物车实战-加入购物车接口开发—小滴

    高并发下的互联网电商购物车实战-加入购物车接口开发 简介:电商购物车实现案例-加入购物车接口开发 添加购物车接口 ...

  • day11购物车10-细节完善

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11购物车08-代理的简单实现

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11-购物车06-清空和购买

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11购物车07-KVO的应用

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • day11购物车09-代理设计模式

    购物车01-搭建基本骨架购物车02-圆角按钮处理购物车03-显示数据购物车04-加号减号点击处理购物车05-通知的...

  • 8.购物车管理

    购物车管理模块是属于用户侧模块,主要有7个接口:添加商品到购物车、更新购物车商品数、移除购物车商品、查看购物车当中...

网友评论

      本文标题:购物车

      本文链接:https://www.haomeiwen.com/subject/srrspttx.html