iOS_一个购物车的使用

这个项目是本人原创:要转载,请说明下:http://www.cnblogs.com/blogwithstudyofwyn/p/5618107.html

项目的地址:https://github.com/Shangshanroushui/ShoppingCart.git

该程序是个一元夺宝的的购物车。

项目中依然使用的MVC

model:是商品的全部信息

GoodsInfoModel.h

@property(strong,nonatomic)NSString *imageName;//商品图片
@property(strong,nonatomic)NSString *goodsTitle;//商品标题
@property(strong,nonatomic)NSString *goodsPrice;//商品单价
@property(assign,nonatomic)BOOL selectState;//是否选中状态
@property(assign,nonatomic)NSInteger goodsNum;//商品个数
@property(assign,nonatomic)NSInteger allNum;//全部个数
@property(assign,nonatomic)NSInteger remainedNum;//还需个数
-(instancetype)initWithDict:(NSDictionary *)dict;

GoodsInfoModel.m

-(instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init])
    {
        self.imageName = dict[@"imageName"];
        self.goodsTitle = dict[@"goodsTitle"];
        self.goodsPrice = dict[@"goodsPrice"];
        self.goodsNum = [dict[@"goodsNum"]integerValue];
        self.selectState = [dict[@"selectState"]boolValue];
        self.allNum=[dict[@"allNum"]integerValue];
        self.remainedNum=[dict[@"remainedNum"]integerValue];
    }

    return  self;
}

View :1.自定义的一个tableViewCell 2.自定义的一个结算View

ShopCartCell.h  :1.用于展示内容的各种控件 2.自定义了一个协议和代理 3.一个按钮触发事件

PS:英文使用了masonry 自动布局。将需要的一些头文件和宏定义放到pch文件中了。直接导入pch文件了

#import "GoodsInfoModel.h"

//添加代理,用于按钮加减的实现
@protocol ShopCartCellDelegate <NSObject>
-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag;
@end
@interface ShopCartCell : UITableViewCell
//增加一个view
@property (nonatomic,strong) UIView *mainView;
@property (nonatomic,strong) UIButton *selectBtn;
@property (nonatomic,strong) UIImageView *goodsImg;
@property (nonatomic,strong) UILabel *introductionLab;
@property (nonatomic,strong) UILabel *needLab;
@property (nonatomic,strong) UILabel *needNumLab;
@property (nonatomic,strong) UILabel *remainedLab;
@property (nonatomic,strong) UILabel *remianedNumLab;
@property (nonatomic,strong) UIButton *addBtn;
@property (nonatomic,strong) UITextField *goodsNumTF;
@property (nonatomic,strong) UIButton *minusBtn;
@property (nonatomic,strong) UILabel *winLab;
@property (nonatomic,strong) UILabel *winNumLab;
@property(assign,nonatomic)BOOL selectState;//选中状态
//赋值
-(void)addTheValue:(GoodsInfoModel *)goodsModel;
@property(assign,nonatomic)id<ShopCartCellDelegate>delegate;

ShopCartCell.m


 #import "SC.pch"

@interface ShopCartCell()

@end
@implementation ShopCartCell

- (void)awakeFromNib {
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.layer.borderColor = [UIColor redColor].CGColor;
//        self.layer.borderWidth = 1;
        [self setSelectionStyle:UITableViewCellSelectionStyleNone];
//        self.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:0.5];
//        [self setSeparatorInset:UIEdgeInsetsMake(0, 60, 0, 0)];
        UIView *mainView=[[UIView alloc]init];
        [self.contentView addSubview:mainView];
        mainView.backgroundColor=[UIColor whiteColor];
        self.mainView=mainView;

        UIButton *selectBtn = [[UIButton alloc]init];
        [self.mainView addSubview:selectBtn];
        [selectBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
        [selectBtn setImage:[UIImage imageNamed:@"5"]forState:UIControlStateSelected];
        [selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        selectBtn.tag=13;
        self.selectBtn=selectBtn;

        UIImageView *goodsImg = [[UIImageView alloc]init];
        [self.mainView addSubview:goodsImg];
//        goodsImg.image = [UIImage imageNamed:@"4"];
        self.goodsImg=goodsImg;

        UILabel *introductionLab = [[UILabel alloc]init];
        [introductionLab setFont:[UIFont systemFontOfSize:17.0]];
        [self.mainView addSubview:introductionLab];
//        [email protected]"[111111111]";
        introductionLab.textColor=[UIColor grayColor];
        self.introductionLab=introductionLab;

        UILabel *needLab = [[UILabel alloc]init];
        [needLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:needLab];
        [email protected]"总需:";
        needLab.textColor=[UIColor grayColor];
        self.needLab=needLab;

        UILabel *needNumLab = [[UILabel alloc]init];
        [needNumLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:needNumLab];
//        [email protected]"1000";
        needNumLab.textColor=[UIColor grayColor];
        self.needNumLab=needNumLab;

        UILabel *remainedLab = [[UILabel alloc]init];
        [remainedLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:remainedLab];
        remainedLab.textColor=[UIColor grayColor];
        [email protected]"剩余:";
        self.remainedLab=remainedLab;

        UILabel *remianedNumLab = [[UILabel alloc]init];
        [remianedNumLab setFont:[UIFont systemFontOfSize:13.0]];
        [self.mainView addSubview:remianedNumLab];
//        [email protected]"999";
        remianedNumLab.textColor=[UIColor redColor];
        self.remianedNumLab=remianedNumLab;

        UIButton *minusBtn = [[UIButton alloc]init];
        [self.mainView addSubview:minusBtn];
        [minusBtn setImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
        [minusBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        minusBtn.tag = 11;
        self.minusBtn=minusBtn;

        UITextField *goodsNumTF = [[UITextField alloc]init];
        [self.mainView addSubview:goodsNumTF];
        goodsNumTF.textAlignment=NSTextAlignmentCenter;
//        [email protected]"10";
        goodsNumTF.backgroundColor=[UIColor grayColor];
        self.goodsNumTF=goodsNumTF;

        UIButton *addBtn = [[UIButton alloc]init];
        [self.mainView addSubview:addBtn];
        [addBtn setImage:[UIImage imageNamed:@"3"] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        addBtn.tag = 12;
        self.addBtn=addBtn;

        UILabel *winLab = [[UILabel alloc]init];
        [winLab setFont:[UIFont systemFontOfSize:14.0]];
        [self.mainView addSubview:winLab];
        [email protected]"中奖概率";
        winLab.textColor=[UIColor grayColor];
        self.winLab=winLab;

        UILabel *winNumLab = [[UILabel alloc]init];
        [winNumLab setFont:[UIFont systemFontOfSize:14.0]];
        [self.mainView addSubview:winNumLab];
        [email protected]"100%";
        winNumLab.textColor=[UIColor grayColor];
        self.winNumLab=winNumLab;

    }
    return self;
}
/**

 *  给单元格赋值

 *

 *  @param goodsModel 里面存放各个控件需要的数值

 */

-(void)addTheValue:(GoodsInfoModel *)goodsModel
{
    self.goodsImg.image = [UIImage imageNamed:goodsModel.imageName];
    self.introductionLab.text = goodsModel.goodsTitle;
    self.needNumLab.text = [NSString stringWithFormat:@"%ld",(long)goodsModel.allNum];
    self.remianedNumLab.text = [NSString stringWithFormat:@"%ld",(long)goodsModel.remainedNum];
    self.goodsNumTF.text=[NSString stringWithFormat:@"%ld",(long)goodsModel.goodsNum];

    if (goodsModel.selectState){
        self.selectState = YES;
        [self.selectBtn setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
    }else{

         self.selectState = NO;
        [self.selectBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    }
}
//
-(void)selectBtnAction:(UIButton *)sender{

    [self.delegate btnClick:self andFlag:(int)sender.tag];
}
//
-(void)deleteBtnAction:(UIButton *)sender{
//    NSInteger goodsNum=[self.goodsNumTF.text integerValue];
//    if (goodsNum>1) {
//        goodsNum-=1;
//        self.goodsNumTF.text=[NSString stringWithFormat:@"%ld",(long)goodsNum];
//        NSInteger remianedNum=[self.remianedNumLab.text integerValue]+1;
//        self.remianedNumLab.text=[NSString stringWithFormat:@"%ld",(long)remianedNum];
//    }
    ////////
    //判断是否选中,选中才能点击
    if (self.selectState == YES)
    {
        //调用代理
        [self.delegate btnClick:self andFlag:(int)sender.tag];
    }
}

-(void)addBtnAction:(UIButton *)sender{
//    NSInteger goodsNum=[self.goodsNumTF.text integerValue];
//    if ([self.remianedNumLab.text integerValue]>0) {
//        goodsNum+=1;
//        self.goodsNumTF.text=[NSString stringWithFormat:@"%ld",(long)goodsNum];
//        NSInteger remianedNum=[self.remianedNumLab.text integerValue]-1;
//        self.remianedNumLab.text=[NSString stringWithFormat:@"%ld",(long)remianedNum];
//    }
    ///////
    if (self.selectState == YES)
    {
        //调用代理
        [self.delegate btnClick:self andFlag:(int)sender.tag];
    }
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    WEAKSELF(weakSelf);
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.mas_equalTo(weakSelf).offset(0);
        make.left.and.right.mas_equalTo(0);
        make.top.and.bottom.mas_equalTo(weakSelf).offset(10);
    }];
    [self.selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mainView.mas_centerY).offset(0);
        make.left.mas_equalTo(10);
        //make.height.and.width.mas_equalTo(17);
        make.height.mas_equalTo(17);
        make.width.mas_equalTo(17);
    }];

    [self.goodsImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mainView.mas_centerY).offset(0);
        make.left.mas_equalTo(weakSelf.selectBtn.mas_right).offset(10);
        //make.height.and.width.mas_equalTo(17);
        make.height.mas_equalTo(70);
        make.width.mas_equalTo(70);
    }];

    [self.introductionLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.mainView.mas_top).offset(10);
        make.left.mas_equalTo(weakSelf.goodsImg.mas_right).offset(10);
        make.right.mas_equalTo(weakSelf.mas_right).offset(0);
        make.height.mas_equalTo(20);
    }];

    [self.needLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.introductionLab.mas_bottom).offset(10);
        make.left.mas_equalTo(weakSelf.introductionLab).offset(0);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(40);
    }];

    [self.needNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab).offset(0);
        make.left.mas_equalTo(weakSelf.needLab.mas_right).offset(0);
        make.height.mas_equalTo(weakSelf.needLab.mas_height).offset(0);
        make.width.mas_equalTo(60);
    }];

    [self.remainedLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab).offset(0);
        make.left.mas_equalTo(weakSelf.needNumLab.mas_right).offset(10);
        make.height.mas_equalTo(weakSelf.needLab.mas_height);
        make.width.mas_equalTo(40);
    }];

    [self.remianedNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab).offset(0);
        make.left.mas_equalTo(weakSelf.remainedLab.mas_right).offset(0);
        make.height.mas_equalTo(weakSelf.needLab.mas_height);
        make.width.mas_equalTo(60);
    }];
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.needLab.mas_bottom).offset(10);
        make.left.mas_equalTo(weakSelf.introductionLab).offset(0);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(24);
    }];
    ////////////////

    [self.winLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.minusBtn).offset(0);
        make.right.mas_equalTo(weakSelf.mas_right).offset(-50);
        make.height.mas_equalTo(10);
        make.width.mas_equalTo(60);
    }];

    [self.winNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.winLab.mas_bottom).offset(5);
        make.right.mas_equalTo(weakSelf.winLab).offset(0);
        make.height.mas_equalTo(weakSelf.winLab);
        make.width.mas_equalTo(weakSelf.winLab);
    }];
    [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.minusBtn).offset(0);
        make.right.mas_equalTo(weakSelf.winLab.mas_left).offset(-20);
        make.height.mas_equalTo(weakSelf.minusBtn);
        make.width.mas_equalTo(weakSelf.minusBtn);
    }];

    [self.goodsNumTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.minusBtn).offset(0);
        make.right.mas_equalTo(weakSelf.addBtn.mas_left).offset(-10);
        make.left.mas_equalTo(weakSelf.minusBtn.mas_right).offset(10);
        make.height.mas_equalTo(weakSelf.minusBtn);
    }];
}

SettlementView.h  一个结算的view ,用于显示选择的个数和金额,以及里那个按钮:全选和结算按钮。

@interface SettlementView : UIView
@property(nonatomic,strong)UIButton*allSelecteBtn;
@property(nonatomic,strong)UILabel*allSelecteLab;//全选
@property(nonatomic,strong)UILabel*sumLab;//总价
@property(nonatomic,strong)UILabel*goodsNumLab;//商品数
@property(nonatomic,strong)UIButton *statementBtn;//结算
@end

SettlementView.m 

#import "SC.pch"
@implementation SettlementView

-(instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if (self){
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
    self.backgroundColor=[UIColor whiteColor];
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = 0.5;
    //全选按钮
    //self.allSelecteBtn
    UIButton *allSelecteBtn=[[UIButton alloc]init];
    [self addSubview:allSelecteBtn];
    [allSelecteBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    _allSelecteBtn=allSelecteBtn;

    [self.allSelecteBtn setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
//    [self.allSelecteBtn setImage:[UIImage imageNamed:@"5"] forState:UIControlStateSelected];

    self.allSelecteLab=[[UILabel alloc]init];
    [self addSubview:self.allSelecteLab];
    [email protected]"全选";
    self.allSelecteLab.font=[UIFont systemFontOfSize:14.0];

    self.sumLab=[[UILabel alloc]init];
    [self addSubview:self.sumLab];
    [email protected]"0 元";
    self.sumLab.textColor=[UIColor redColor];
    self.allSelecteLab.font=[UIFont systemFontOfSize:16.0];

    self.goodsNumLab=[[UILabel alloc]init];
    [self addSubview:self.goodsNumLab];
    [email protected]"共计:0 件商品";
    self.goodsNumLab.font=[UIFont systemFontOfSize:12.0];

    //全选按钮
    self.statementBtn=[[UIButton alloc]init];
    [self addSubview:self.statementBtn];
    [self.statementBtn setBackgroundColor:[UIColor redColor]];
    [self.statementBtn setTitle:@"结算" forState:UIControlStateNormal];
}
-(void)selectBtnAction:(UIButton *)sender{

    //[self.delegate btnClick:self andFlag:(int)sender.tag];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    WEAKSELF(weakSelf);
    [self.allSelecteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY).offset(0);
        make.left.mas_equalTo(weakSelf).offset(10);
        make.height.mas_equalTo(17);
        make.width.mas_equalTo(17);
    }];

    [self.allSelecteLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY).offset(0);
        make.left.mas_equalTo(weakSelf.allSelecteBtn.mas_right).offset(5);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    [self.sumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.mas_equalTo(weakSelf.allSelecteLab.mas_right).offset(20);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(100);
    }];
    [self.goodsNumLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.sumLab.mas_bottom).offset(5);
        make.left.mas_equalTo(weakSelf.sumLab);
        make.height.mas_equalTo(weakSelf.sumLab);
        make.width.mas_equalTo(weakSelf.sumLab);
    }];
    [self.statementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.mas_centerY).offset(0);
        make.right.mas_equalTo(weakSelf.mas_right).offset(-5);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(100);
    }];
}

Controller:

ShopCartViewController.h

ShopCartViewController.m  

项目最核心:选择商品,添加,减少等,以及在这个操作中结算View的变化。

#import "ShopCartViewController.h"
#import "ShopCartCell.h"
#import "SC.pch"
#import "SettlementView.h"
#import "GoodsInfoModel.h"

#import "StatementViewController.h"
@interface ShopCartViewController ()<UITableViewDataSource,UITableViewDelegate,ShopCartCellDelegate>
@property(nonatomic,strong)UITableView *goodsTableView;
@property(nonatomic,strong)SettlementView *settlementView;
@property(nonatomic,assign)NSInteger allPrice;
@property(nonatomic,assign)NSInteger goodsNum;;
@property(nonatomic,strong)NSMutableArray *infoArr;;
@end
@implementation ShopCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
    [self setInfo];
    [self setGoodsTableView];
    [self setupSettlement];

}
-(void)setInfo{
    self.allPrice=0;
    self.goodsNum = 0;
    self.infoArr = [[NSMutableArray alloc]init];
    /**

     *  初始化一个数组,数组里面放字典。字典里面放的是单元格需要展示的数据

     */

    for (int i = 0; i<7; i++)

    {

        NSMutableDictionary *infoDict = [[NSMutableDictionary alloc]init];

        [infoDict setValue:@"4.png" forKey:@"imageName"];

        [infoDict setValue:@"这是商品标题" forKey:@"goodsTitle"];

//        [infoDict setValue:@"2000" forKey:@"goodsPrice"];

        [infoDict setValue:[NSNumber numberWithBool:NO] forKey:@"selectState"];

        [infoDict setValue:[NSNumber numberWithInt:1] forKey:@"goodsNum"];
        /*
         @property(assign,nonatomic)int allNum;

         @property(assign,nonatomic)int remainedNum;
         */
        [infoDict setValue:[NSNumber numberWithInt:1000] forKey:@"allNum"];
        [infoDict setValue:[NSNumber numberWithInt:999] forKey:@"remainedNum"];

        //封装数据模型
        GoodsInfoModel *goodsModel = [[GoodsInfoModel alloc]initWithDict:infoDict];

        //将数据模型放入数组中

        [self.infoArr addObject:goodsModel];

    }
}
-(void)setupSettlement{
    SettlementView *settlementView=[[SettlementView alloc]initWithFrame:CGRectMake(0, kScreen_Height-100, kScreen_Width, 60)];
    [settlementView.statementBtn addTarget:self action:@selector(goStatement) forControlEvents:UIControlEventTouchUpInside];

    [settlementView.allSelecteBtn addTarget:self action:@selector(goAllSelect:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:settlementView];
    self.settlementView=settlementView;

}
-(void)setGoodsTableView{
    UITableView *goodsTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-20)];//0, 44, kScreen_Width, kScreen_Height-20)
    [self.view addSubview:goodsTableView];
    goodsTableView.showsHorizontalScrollIndicator=NO;
    goodsTableView.showsVerticalScrollIndicator=NO;
//    goodsTableView.backgroundColor=[UIColor colorWithRed:234/255.0 green:234/255.0 blue:234/255.0 alpha:1];
    goodsTableView.backgroundColor=[UIColor redColor];
    goodsTableView.delegate=self;
    goodsTableView.dataSource=self;
    [goodsTableView registerClass:[ShopCartCell class] forCellReuseIdentifier:@"SCCell"];
//    [goodsTableView registerNib:[UINib nibWithNibName:@"ShopCartTableCell" bundle:nil] forCellReuseIdentifier:@"SCCell"];
    goodsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.goodsTableView=goodsTableView;
}

#pragma mark-------
#pragma mark--------tableViewDelegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *SCCell =  @"SCCell";
    ShopCartCell *cell=[tableView dequeueReusableCellWithIdentifier:SCCell forIndexPath:indexPath];
    if (!cell){
        cell=[[ShopCartCell alloc]initWithStyle:UITableViewCellStyleDefault  reuseIdentifier:SCCell];
    }
    cell.delegate = self;
    [cell addTheValue:self.infoArr[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.infoArr.count;
}

#pragma mark -- 实现加减按钮点击代理事件
/**
 *  实现加减按钮点击代理事件
 *
 *  @param cell 当前单元格
 *  @param flag 按钮标识,11 为减按钮,12为加按钮
 */
-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag{
    NSIndexPath *index = [self.goodsTableView indexPathForCell:cell];
    switch (flag) {
        case 11:
        {
            //做减法
            //先获取到当期行数据源内容,改变数据源内容,刷新表格
            GoodsInfoModel *model = self.infoArr[index.row];
            if (model.goodsNum > 1)
            {
                model.goodsNum --;
                model.remainedNum++;
            }
           // [self.goodsTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 12:
        {
            //做加法
            GoodsInfoModel *model = self.infoArr[index.row];
            if (model.remainedNum>0) {
                model.remainedNum --;
                model.goodsNum++;
            }
            //[self.goodsTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
            break;
        case 13:
        {    //先获取到当期行数据源内容,改变数据源内容,刷新表格
            GoodsInfoModel *model = self.infoArr[index.row];
            if (model.selectState)            {
                model.selectState = NO;
            }else {
                model.selectState = YES;

            }
            //刷新当前行
            //[self.goodsTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
        default:
            break;
    }
    //刷新表格
    [self.goodsTableView reloadData];

    //计算总价
    [self totalPrice];

}
#pragma mark -- 计算价格
-(void)totalPrice
{
    //遍历整个数据源,然后判断如果是选中的商品,就计算价格(单价 * 商品数量)
    for ( int i =0; i<self.infoArr.count; i++)    {
        GoodsInfoModel *model = [self.infoArr objectAtIndex:i];
        if (model.selectState)
        {
            self.allPrice +=  model.goodsNum ;//[model.goodsPrice intValue];
            self.goodsNum +=model.goodsNum;
        }
    }
    //给总价文本赋值
    self.settlementView.sumLab.text = [NSString stringWithFormat:@"%ld 元",(long)self.allPrice];
    self.settlementView.goodsNumLab.text=[NSString stringWithFormat:@"共计:%ld 件商品 ",(long)self.goodsNum];
    //每次算完要重置为0,因为每次的都是全部循环算一遍
    self.allPrice = 0;
    self.goodsNum = 0;
}
//结算
-(void)goStatement{
    NSInteger total=[self.settlementView.sumLab.text integerValue];
    if (total>0) {
        StatementViewController *statementVC=[[StatementViewController alloc]init];
        statementVC.price=total;
        [self.navigationController pushViewController:statementVC animated:YES];

    }
}
//全选
-(void)goAllSelect:(UIButton *)sender{
    //判断是否选中,是改成否,否改成是,改变图片状态
    sender.tag = !sender.tag;
    if (sender.tag)    {
        [sender setImage:[UIImage imageNamed:@"5.png"] forState:UIControlStateNormal];

    }else{
        [sender setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
    }
    //改变单元格选中状态
    for (int i=0; i<self.infoArr.count; i++)
    {
        GoodsInfoModel *model = [self.infoArr objectAtIndex:i];
        model.selectState = sender.tag;
    }
    //计算价格
    [self totalPrice];
    //刷新表格
    [self.goodsTableView reloadData];

}

StatementViewController.h 结算试图

@property(nonatomic,assign)NSInteger price;//总价

StatementViewController.m

#import "StatementViewController.h"
#import "SC.pch"
@interface StatementViewController ()
@end

@implementation StatementViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];

    UILabel *allPrice=[[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 160)];
    allPrice.text= [NSString stringWithFormat:@"-----%ld-------",(long)self.price];
    [self.view addSubview:allPrice];

}
时间: 2024-10-20 16:23:47

iOS_一个购物车的使用的相关文章

利用html5的本地存储写的一个购物车

好久没有写博客园了,很多知识没有记录下来:可惜: 这几天在开发微信,也写了一个订餐平台的微网站,里面需要写一个购物车: 这里主要是把商品的部分信息以json格式保存在sessionstorage中,还有商店信息也是: 以json格式保存有什么好处呢,轻量级的传输,大概是这样吧!另外,如果我们把商品信息分开存储,就会导致有多条的sessionstorage项,那以后实现在两家商店同时购物的话,就不可能区分每家商店的商品了: 如果代码是自己写的,就有版权,这么说.对吧,是在软件工程师书上看到的: 不

分享一个购物车的demo(效果高仿饿了么软件的购物效果)

这次分享的是一个很常见的效果,凡是涉及到购物的app或者旅游类以及订餐类的app都有看到过这种效果,两个list view联动显示,添加购物车时的一个抛物线动画实现,以及图标或者item右上角的数字显示.下面是我空闲时候写的一个demo,界面比较陋,但是基本效果都实现了. 不多说,看效果图: 只对添加按钮做了监听,删除没去写(没什么必要). 讲一些主要的代码,想下载源码的可以去这里下载(https://github.com/bobge/LinkedListView.git): 左边的listvi

Vue写一个购物车

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.0.2/css/bootstrap.min.css"/> </head> <body> <div id="app&

前端开发:Vue写一个购物车

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> https://cdn.bootcss.com/bootstrap/3.0.2/css/bootstrap.min.css"/> </head> <body> <div id="app"> <table> <thead> <tr>

12月16日 增加一个购物车内product数量的功能, 自定义method,在helper中定义,计算代码Refactor到Model中。

仿照Rails实战:购物网站 教材:5-6 step5:计算总价,做出在nav上显示购物车内product的数量. 遇到的?: 1. <% sum = 0 %> <% current_cart.cart_items each do |cart_item| %> <% if cart_item.product.price.present? %> <% sum = sum + cart_item.quantity * cart_item.product.price %

jQuery实现一个简单的购物车功能

最近由于工作需要的原因,开始系统学习jQuery的知识,然后跟着一个视频教程做了一个购物车的功能,现总结如下. 第一步:准备HTML页面,代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.

用java代码写一个简单的网上购物车程序

1 需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 2 3 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修改商品,清空购物车,求购物车中所有商品总金额方法.4.写一个测试类,测试上述方法. 4 5 商品类: 6 [java] view plain copy 7 8 public class Product { 9 private int productId;// 商品编号 10 privat

从头开始编写一个Orchard网上商店模块(6) - 创建购物车服务和控制器

原文地址: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-6创建购物车服务和控制器 这是从头开始编写一个新的Orchard模块的教程的第6篇.对于本教程的概述,请参阅介绍. 在本篇,我们将使我们的用户可以添加商品到他们的购物车.要创建这样的功能,我们需要: 一个“添加到购物车”按钮,要被添加我们的产品目录上,将产品添加到购物车 某种购物车服务,以存储

模拟一个全局悬浮的购物车

https://www.zybuluo.com/keenleung/note/339988 所有的界面中,都悬浮一个购物车 思路: 在 AppDelegate 中创建多一个 Widow, 而且设置 Window 的级别是最高的 注意: 不要指定 Window 的根控制器,指定了根控制器的话,状态栏的显示样式就会与这个根控制器决定了,因为状态栏的显示样式, 是由最顶层 Window 的根控制器决定的 Window 的显示不需要添加到任何地方,只需要设置 hidden 属性的值为 NO 即可 做法: