星空网 > 软件开发 > 操作系统

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];//    introductionLab.text=@"[111111111]";    introductionLab.textColor=[UIColor grayColor];    self.introductionLab=introductionLab;        UILabel *needLab = [[UILabel alloc]init];    [needLab setFont:[UIFont systemFontOfSize:13.0]];    [self.mainView addSubview:needLab];    needLab.text=@"总需:";    needLab.textColor=[UIColor grayColor];    self.needLab=needLab;            UILabel *needNumLab = [[UILabel alloc]init];    [needNumLab setFont:[UIFont systemFontOfSize:13.0]];    [self.mainView addSubview:needNumLab];//    needNumLab.text=@"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];    remainedLab.text=@"剩余:";    self.remainedLab=remainedLab;        UILabel *remianedNumLab = [[UILabel alloc]init];    [remianedNumLab setFont:[UIFont systemFontOfSize:13.0]];    [self.mainView addSubview:remianedNumLab];//    remianedNumLab.text=@"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;//    goodsNumTF.text=@"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];    winLab.text=@"中奖概率";    winLab.textColor=[UIColor grayColor];    self.winLab=winLab;        UILabel *winNumLab = [[UILabel alloc]init];    [winNumLab setFont:[UIFont systemFontOfSize:14.0]];    [self.mainView addSubview:winNumLab];    winNumLab.text=@"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];  self.allSelecteLab.text=@"全选";  self.allSelecteLab.font=[UIFont systemFontOfSize:14.0];    self.sumLab=[[UILabel alloc]init];  [self addSubview:self.sumLab];  self.sumLab.text=@"0 元";  self.sumLab.textColor=[UIColor redColor];  self.allSelecteLab.font=[UIFont systemFontOfSize:16.0];    self.goodsNumLab=[[UILabel alloc]init];  [self addSubview:self.goodsNumLab];  self.goodsNumLab.text=@"共计: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];  }

 




原标题:iOS_一个购物车的使用

关键词:IOS

IOS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流