这个项目是本人原创:要转载,请说明下: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_一个购物车的使用的更多相关文章

  1. 用vuex写了一个购物车H5页面的示例代码

    用vuex写了一个购物车H5页面的示例代码:https://www.jb51.net/article/152008.htm 通过购物车的一个案列,把vuex学习了一篇. vuex概念浅谈 Vuex 是 ...

  2. vue.js移动端app实战3:从一个购物车入门vuex

    什么是vuex? 官方的解释是:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 简单来说就 ...

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

    仿照Rails实战:购物网站 教材:5-6 step5:计算总价,做出在nav上显示购物车内product的数量. 遇到的❌: 1. <% sum = 0 %> <% current ...

  4. vue-实现一个购物车结算页面

    这是路由之间的跳转,传递值最好采用传参,而不是用$emit和$on,不起作用 如果实在一个页面中的兄弟组件,可以使用$emit和$on 中间件,eventBus.js 放在components目录下面 ...

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

    原文地址: http://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-pa ...

  6. 跟我学ASP.NET MVC之七:SportsStrore一个完整的购物车

    摘要: SportsStore应用程序进展很顺利,但是我不能销售产品直到设计了一个购物车.在这篇文章里,我就将创建一个购物车. 在目录下的每个产品旁边添加一个添加到购物车按钮.点击这个按钮将显示客户到 ...

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

    需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修 ...

  8. session中删除数组中的某一个值 - 购物车例子 - jsp

    这篇随笔简单的讲一下在session中移除数组中的某一项内容,比如这里有一个购物车其中有两件商品,需要移除其中洗发水这一件商品. 其实在这个session对象中存储了一个数组,在订购页面时选择商品加入 ...

  9. 如何一步一步用DDD设计一个电商网站(四)—— 把商品卖给用户

    阅读目录 前言 怎么卖 领域服务的使用 回到现实 结语 一.前言 上篇中我们讲述了“把商品卖给用户”中的商品和用户的初步设计.现在把剩余的“卖”这个动作给做了.这里提醒一下,正常情况下,我们的每一步业 ...

随机推荐

  1. JavaScript增强AJAX基础

    <title>js类型</title> <meta http-equiv="content-type" content="text/html ...

  2. 使用CTE减少统计子查询

    Set Statistics IO ON SET STATISTICS TIME ON --/*--原来语句 DECLARE @CkNo nvarchar(4000),@ProWarn int,@Sk ...

  3. svn 架设

    1.yum install subversion  openssl-devel -y 2. cd /data/svn 3. svnadmin create remote 4. 编辑conf 下 aut ...

  4. WPF绑定xml数据源

    1.界面 <UserControl x:Class="HKDCMS.Client.Demo.UIViews.UIControls.AboutUsControl" xmlns= ...

  5. iOS 如何在Label中显示html的文本

    if (self.messageModel) { NSString * htmlString = self.messageModel.contentText; NSAttributedString * ...

  6. css名词解释

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. JQ 数字验证

    $.fn.extend({ checknum: function (min, max, accurate) { if ($(this).val() != "") { $(this) ...

  8. js 控制浏览器窗口大小

    //打开一个新窗口并设置其大小window.open('index.html','','width=450,height=750,location=no,menubar=no,status=no,to ...

  9. linux 连接到阿里云服务器

    当Windows拥有xshell软件可以连接到你的远程服务器时,Linux其实自己带有的ssh就可以连接: 具体命令是: ssh root@60.2.5.201.81然后输入你服务器的密码:××××× ...

  10. 配置ADFS Outlook Client.

     error:The authentication endpoint Username was not found on the configured Secure Token Service!1.  ...