项目需要selectoriOS Developer

两个代理方法实现iOS购物车2-完整版

2017-03-10  本文已影响314人  马铃薯蜀黍
首页图

上次写完购物车精简版被编辑推荐到了首页,得到许多朋友关注,评论区小伙伴建议做一个完整版.于是利用闲暇时间新增了一些功能:

github代码:https://github.com/zhYes/YSShoppingCar-Complite.git

全选按钮逻辑效果:

全选效果展示

part one 全选按钮有关代码

代理方法1中伪代码:

#pragma mark - cell上的代理方法
- (void)shopCellSelectedClick:(NSInteger)shopCellTag {
....
///插入判断代码
//判断  //cell改变组头 //组头改变全选
            NSInteger selectedNum = 0 ;
            for (MKOrderListModel * tempListModel in _modelArray) {//遍历所有组
                if (tempListModel.groupSelected) {//如果组头是选中的
                    selectedNum += 1;
                }
                if (selectedNum == _modelArray.count) {
                    allSelectBtn.selected = YES;
                }
            }
        } else {
            listModel.groupSelected = NO;
            allSelectBtn.selected = NO;
        }
....
}

cell的选中增加了判断代码,决定全选按钮的状态

完整代码:

#pragma mark - cell上的代理方法获 | 取的价格
- (void)shopCellSelectedClick:(NSInteger)shopCellTag {
    
    //判断组的是否选中状态是否修改
    NSString * cellTagStr = [NSString stringWithFormat:@"%zd",shopCellTag];
    NSIndexPath *indexPath = self.dic[cellTagStr];
    MKOrderListModel * listModel = (MKOrderListModel*)_modelArray[indexPath.section];
    //0.便利当前组cell上选中按钮的个数
    NSInteger seletedNum =0;
    for (MKGoodsModel* goodsModel in listModel.goods) {
        if (goodsModel.isSelected) {
            seletedNum += 1;
        }
        // 1.当前组的cell的个数 是否等于 勾选的总数
        if (((MKOrderListModel*)_modelArray[indexPath.section]).goods.count == seletedNum) {
            listModel.groupSelected = YES; //cell改变组头变为选中
            //判断  //cell改变组头 //组头改变全选
            NSInteger selectedNum = 0 ;
            for (MKOrderListModel * tempListModel in _modelArray) {//遍历所有组
                if (tempListModel.groupSelected) {//如果组头是选中的
                    selectedNum += 1;
                }
                if (selectedNum == _modelArray.count) {
                    allSelectBtn.selected = YES;
                }
            }
        } else {
            listModel.groupSelected = NO;
            allSelectBtn.selected = NO;
        }
        [_tableView reloadData];
    }
    
    MKGoodsModel *goodsModel = ((MKOrderListModel*)_modelArray[indexPath.section]).goods[indexPath.row];
    float goods_price = goodsModel.goods_price;
    float goods_number = goodsModel.goods_number;
    if (!goodsModel.isSelected) {
        _totalNum = _totalNum - goods_price*goods_number;
//        NSLog(@"%.2f",_totalNum);
        
    }else {
        
        _totalNum = _totalNum + goods_price*goods_number;
//        NSLog(@"%.2f",_totalNum);
    }
    _hejiLabel.text = [NSString stringWithFormat:@"¥%.2f",_totalNum -1 + 1];
    if ([_hejiLabel.text containsString:@"-"]) {
        _hejiLabel.text = @"¥0.0";
    }
}

当前cell选中时候,当前组头也选中的时候,判断当前所有选中组的个数是否等于全部组数

代理方法2中伪代码:

#pragma mark - 代理方法组头header的选中状态
- (void)headerSelectedBtnClick:(NSInteger)section {
...
插入判断
// 判断如果点击 | header选中
    if (listModel.groupSelected) {

        //    /// 判断组头的点击改变全选按钮
        NSInteger tempGroupSelectNum = 0;
        for (MKOrderListModel *model in _modelArray) {
            if (model.groupSelected) {
                tempGroupSelectNum ++;
            }
            if (tempGroupSelectNum == _modelArray.count) {
                allSelectBtn.selected = YES;
            }
        }
...
}

数组的选中按钮中增加判断

完整代码:

#pragma mark - 代理方法组头header的选中状态
- (void)headerSelectedBtnClick:(NSInteger)section {
    //    NSLog(@"%zd",section);
    MKOrderListModel*listModel = _modelArray[section];
    listModel.groupSelected = !listModel.groupSelected;

    
    // 判断如果点击 | header选中
    if (listModel.groupSelected) {
        
        //    /// 判断组头的点击改变全选按钮
        NSInteger tempGroupSelectNum = 0;
        for (MKOrderListModel *model in _modelArray) {
            if (model.groupSelected) {
                tempGroupSelectNum ++;
            }
            if (tempGroupSelectNum == _modelArray.count) {
                allSelectBtn.selected = YES;
            }
        }
        
        
        for (MKGoodsModel* goodsModel in listModel.goods) {
            
            if (!goodsModel.isSelected) {                                       //下面不是选中状态的cell 将价格加入到总价当中
                float goods_price = goodsModel.goods_price;               //价格
                float goods_number = goodsModel.goods_number;   // 数量
                _totalNum += goods_price * goods_number;
                goodsModel.isSelected = YES;
            }
            
        }
    } else {  // 取消header选中 所有都取消
        //全选按钮变为不选中
        allSelectBtn.selected = NO;
        for (MKGoodsModel* goodsModel in listModel.goods) {
            goodsModel.isSelected = NO;
            float goods_price = goodsModel.goods_price;               //价格
            float goods_number = goodsModel.goods_number;   // 数量
            _totalNum -= goods_price * goods_number;
        }
    }
//    NSLog(@"总价格为: %.2f",_totalNum);
    _hejiLabel.text = [NSString stringWithFormat:@"¥%.2f",_totalNum - 1 + 1];
    if ([_hejiLabel.text containsString:@"-"]) {
        _hejiLabel.text = @"0";
    }
    [_tableView reloadData];
}

全选按钮的点击处理:

/// MARK: 全选的点击事件
- (void)allSelectBtnClick: (UIButton*)allSelectedBtn{
    allSelectedBtn.selected = !allSelectedBtn.selected; // 修改全选按钮的状态
    if (allSelectedBtn.selected) { // 如果全选按钮改变了为选中
        
        for (int i = 0; i <_modelArray.count; i ++) {
            MKOrderListModel * listModel = _modelArray[i];
            if (!listModel.groupSelected) {//遍历如果组不是选中状态
                [self headerSelectedBtnClick:i]; //模拟组头点击了一次
                
            }
        }
    }else { // 全选按钮改变为不选中 那么所有商品都不选中!
        for (int i = 0; i < _modelArray.count; i ++) { // 遍历所有的组头点击
            [self headerSelectedBtnClick:i];
        }
    }
}

点击全选,按钮状态取反
取反后,全选按钮是选中,则遍历所有组把所有未选中的组头做一次点击操作
取反后,全选按钮是未选中,则所有组头做一次点击操作即可

修改数量效果展示:

修改数量效果

part two 修改数量相关代码:

关于购物车中textField的逻辑代码可查看我的另一篇记用了12个if做的textField输入框的变态需求
这里贴一下数量加减的代码:

/// MARK:数量增加按钮
- (IBAction)plusBtnClick {
    if (_ex_buynum_texField.editing) {
        return;
    }
    // 保存之前值
    float beforeBuyNum = _goodsModel.goods_number;
    // 判断不能大于20
    if (_ex_buynum_texField.text.floatValue == 20) {
        return;
    }
    /// 修改模型数据
    _ex_buynum_texField.text = [NSString stringWithFormat:@"%.1f",_ex_buynum_texField.text.floatValue + 0.5];
    _goodsModel.goods_number = _ex_buynum_texField.text.floatValue;
    if ([self.shopDelegate respondsToSelector:@selector(shopCellEndEditerClick:beforeBuyNum:)]) {
        [self.shopDelegate shopCellEndEditerClick:self.tag beforeBuyNum:beforeBuyNum];
    }
    /// 传递给服务器 一会写 $$$$$$$$
}
- (IBAction)reduceBtnClick {
    if (_ex_buynum_texField.editing) {
        return;
    }
    // 保存之前值
    float beforeBuyNum = _goodsModel.goods_number;
    // 判断不能小于0.5
    if (_ex_buynum_texField.text.floatValue == 0.5) {
        return;
    }
    /// 修改模型数据
    _ex_buynum_texField.text = [NSString stringWithFormat:@"%.1f",_ex_buynum_texField.text.floatValue - 0.5];
    _goodsModel.goods_number = _ex_buynum_texField.text.floatValue;
    if ([self.shopDelegate respondsToSelector:@selector(shopCellEndEditerClick:beforeBuyNum:)]) {
        [self.shopDelegate shopCellEndEditerClick:self.tag beforeBuyNum:beforeBuyNum];
    }
    /// 传递给服务器 一会写 $$$$$$$$
}

@end

上一篇下一篇

猜你喜欢

热点阅读