电商项目购物车

self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

[self.window makeKeyAndVisible];

self.window.backgroundColor = [UIColor whiteColor];

ViewController *vc = [[ViewController alloc]init];

UINavigationController *nVC = [[UINavigationController alloc]initWithRootViewController:vc];

self.window.rootViewController = nVC;


#import "ViewController.h"#import "GoodsInfoModel.h"#import "MyCustomCell.h"@interface ViewController (){

UITableView *_MyTableView;

float allPrice;

NSMutableArray *infoArr;

}

@property(strong,nonatomic)UIButton *allSelectBtn;

@property(strong,nonatomic)UILabel *allPriceLab;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.title = @"购物车";

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor  blackColor],NSFontAttributeName:[UIFont systemFontOfSize:23.0f]}];

//初始化数据

allPrice = 0.0;

infoArr = [[NSMutableArray alloc]init];

/**

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

*/

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

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

[infoDict setValue:@"img6.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"];

//封装数据模型

GoodsInfoModel *goodsModel = [[GoodsInfoModel alloc]initWithDict:infoDict];

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

[infoArr addObject:goodsModel];

}

_MyTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];

_MyTableView.dataSource = self;

_MyTableView.delegate = self;

//给表格添加一个尾部视图

_MyTableView.tableFooterView = [self creatFootView];

[self.view addSubview:_MyTableView];

}

-(UIView *)creatFootView{

UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];

//添加一个全选文本框标签

UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 150, 10, 50, 30)];

lab.text = @"全选";

[footView addSubview:lab];

//添加全选图片按钮

_allSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_allSelectBtn.frame = CGRectMake(self.view.frame.size.width- 100, 10, 30, 30);

[_allSelectBtn setImage:[UIImage imageNamed:@"复选框-未选中"] forState:UIControlStateNormal];

[_allSelectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];

[footView addSubview:_allSelectBtn];

//添加小结文本框

UILabel *lab2 = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 150, 40, 60, 30)];

lab2.textColor = [UIColor redColor];

lab2.text = @"小结:";

[footView addSubview:lab2];

//添加一个总价格文本框,用于显示总价

_allPriceLab = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 100, 40, 100, 30)];

_allPriceLab.textColor = [UIColor redColor];

_allPriceLab.text = @"0.0";

[footView addSubview:_allPriceLab];

//添加一个结算按钮

UIButton *settlementBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[settlementBtn setTitle:@"去结算" forState:UIControlStateNormal];

[settlementBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

settlementBtn.frame = CGRectMake(10, 80, self.view.frame.size.width - 20, 30);

settlementBtn.backgroundColor = [UIColor blueColor];

[footView addSubview:settlementBtn];

return footView;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section{

return infoArr.count;

}

//定制单元格内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identify = @"indentify";

MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];

if (!cell){

cell = [[MyCustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];

cell.delegate = self;

}

//调用方法,给单元格赋值

[cell addTheValue:infoArr[indexPath.row]];

return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 120;

}

//单元格选中事件

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

/**

*  判断当期是否为选中状态,如果选中状态点击则更改成未选中,如果未选中点击则更改成选中状态

*/

GoodsInfoModel *model = infoArr[indexPath.row];

if (model.selectState){

model.selectState = NO;

}else{

model.selectState = YES;

}

//刷新整个表格

//[_MyTableView reloadData];

//刷新当前行

[_MyTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

[self totalPrice];

}

-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag

{

NSIndexPath *index = [_MyTableView indexPathForCell:cell];

switch (flag) {

case 11:

{

//做减法

//先获取到当期行数据源内容,改变数据源内容,刷新表格

GoodsInfoModel *model = infoArr[index.row];

if (model.goodsNum > 1)

{

model.goodsNum --;

}

}

break;

case 12:

{

//做加法

GoodsInfoModel *model = infoArr[index.row];

model.goodsNum ++;

}

break;

default:

break;

}

//刷新表格

[_MyTableView reloadData];

//计算总价

[self totalPrice];

}

//全选按钮的选中状态

-(void)selectBtnClick:(UIButton *)sender{

//判断是否选中,是改成否,否改成是,改变图片状态

sender.tag = !sender.tag;

if (sender.tag){

[sender setImage:[UIImage imageNamed:@"复选框-选中.png"]  forState:UIControlStateNormal];

}else{

[sender setImage:[UIImage imageNamed:@"复选框-未选中.png"] forState:UIControlStateNormal];

}

//改变单元格选中状态

for (int i=0; i<infoArr.Count;i++){

GoodsInfoModel *model = [infoArr objectAtIndex:i];

if (model.selectState){

allPrice = allPrice + model.goodsNum *[model.goodsPrice intValue];

}

}

//给总价文本赋值

_allPriceLab.text = [NSString stringWithFormat:@"%.2f",allPrice];

NSLog(@"%f",allPrice);

//每次算完要重置为0,因为每次的都是全部循环算一遍

allPrice = 0.0;

}





#import@interface GoodsInfoModel : NSObject

@property(strong,nonatomic)NSString *imageName;//商品图片

@property(strong,nonatomic)NSString *goodsTitle;//商品标题

@property(strong,nonatomic)NSString *goodsPrice;//商品单价

@property(assign,nonatomic)BOOL selectState;//是否选中状态

@property(assign,nonatomic)int goodsNum;//商品个数

-(instancetype)initWithDict:(NSDictionary *)dict;

@end


#import "GoodsInfoModel.h"

@implementation GoodsInfoModel

-(instancetype)initWithDict:(NSDictionary *)dict{

if(self = [super init]){

self.imageName = dict[@"imageName"];

self.goodsTitle = dict[@"goodsTitle"];

self.goodsPrice = dict[@"goodsPrice"];

self.goodsNum = [dict[@"goodsNum"]intValue];

self.selectState = [dict[@"selectState"]boolValue];

}

return self;

}

@end


#import#import "GoodsInfoModel.h"//添加代理,用于按钮加减的实现@protocol MyCustomCellDelegate-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag;@end@interface MyCustomCell : UITableViewCell@property(strong,nonatomic)UIImageView *goodsImgV;//商品图片@property(strong,nonatomic)UILabel *goodsTitleLab;//商品标题@property(strong,nonatomic)UILabel *priceTitleLab;//价格标签@property(strong,nonatomic)UILabel *priceLab;//具体价格@property(strong,nonatomic)UILabel *goodsNumLab;//购买数量标签@property(strong,nonatomic)UILabel *numCountLab;//购买商品的数量@property(strong,nonatomic)UIButton *addBtn;//添加商品数量@property(strong,nonatomic)UIButton *deleteBtn;//删除商品数量@property(strong,nonatomic)UIButton *isSelectBtn;//是否选中按钮@property(strong,nonatomic)UIImageView *isSelectImg;//是否选中图片@property(assign,nonatomic)BOOL selectState;//选中状态@property(assign,nonatomic)iddelegate;

//赋值

-(void)addTheValue:(GoodsInfoModel *)goodsModel;

@end


//

//  MyCustomCell.m

//  电商项目购物车

//



//

#define WIDTH ([UIScreen mainScreen].bounds.size.width)

#import "MyCustomCell.h"

@implementation MyCustomCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){

//布局界面

UIView * bgView = [[UIView alloc]initWithFrame:CGRectMake(5, 5, WIDTH-10, 95)];

bgView.backgroundColor = [UIColor whiteColor];

//添加商品图片

_goodsImgV = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 80, 80)];

_goodsImgV.backgroundColor = [UIColor greenColor];

[bgView addSubview:_goodsImgV];

//添加商品标题

_goodsTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(90, 5, 200, 30)];

_goodsTitleLab.text = @"afadsfa fa";

_goodsTitleLab.backgroundColor = [UIColor clearColor];

[bgView addSubview:_goodsTitleLab];

//促销价

_priceTitleLab = [[UILabel alloc]initWithFrame:CGRectMake(90, 35, 70, 30)];

_priceTitleLab.text = @"促销价:";

_priceTitleLab.backgroundColor = [UIColor clearColor];

[bgView addSubview:_priceTitleLab];

//商品价格

_priceLab = [[UILabel alloc]initWithFrame:CGRectMake(160, 35, 100, 30)];

_priceLab.text = @"1990";

_priceLab.textColor = [UIColor redColor];

[bgView addSubview:_priceLab];

//购买数量

_goodsNumLab = [[UILabel alloc]initWithFrame:CGRectMake(90, 65, 90, 30)];

_goodsNumLab.text = @"购买数量:";

[bgView addSubview:_goodsNumLab];

//减按钮

_deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_deleteBtn.frame = CGRectMake(180, 65, 30, 30);

_deleteBtn.backgroundColor = [UIColor blueColor];

[_deleteBtn setImage:[UIImage imageNamed:@"按钮-.png"] forState:UIControlStateNormal];

[_deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];

_deleteBtn.tag = 11;

[bgView addSubview:_deleteBtn];

//购买商品的数量

_numCountLab = [[UILabel alloc]initWithFrame:CGRectMake(210, 65, 50, 30)];

_numCountLab.textAlignment = NSTextAlignmentCenter;

[bgView addSubview:_numCountLab];

//加按钮

_addBtn = [UIButton buttonWithType:UIButtonTypeCustom];

_addBtn.frame = CGRectMake(260, 65, 30, 30);

_addBtn.backgroundColor = [UIColor blackColor];

[_addBtn setImage:[UIImage imageNamed:@"按钮+.png"] forState:UIControlStateNormal];

[_addBtn addTarget:self action:@selector(addBtnAction:) forControlEvents:UIControlEventTouchUpInside];

_addBtn.tag = 12;

[bgView addSubview:_addBtn];

//是否选中图片

_isSelectImg = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 50, 10, 30, 30)];

[bgView addSubview:_isSelectImg];

[self addSubview:bgView];

}

return self;

}

/**

*  给单元格赋值

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

*/

-(void)addTheValue:(GoodsInfoModel *)goodsModel{

_goodsImgV.image = [UIImage imageNamed:goodsModel.imageName];

_goodsTitleLab.text = goodsModel.goodsTitle;

_priceLab.text = goodsModel.goodsPrice;

_numCountLab.text = [NSString stringWithFormat:@"%d",goodsModel.goodsNum];

if (goodsModel.selectState){

//单元格选中的时候下面的增加减少按钮就能使用

_selectState = YES;

_isSelectImg.image = [UIImage imageNamed:@"复选框-选中"];

}else{

_selectState = NO;

_isSelectImg.image = [UIImage imageNamed:@"复选框-未选中"];

}

}

#pragma mark -- 减按钮

/**

*  点击减按钮实现数量的减少

*/

-(void)deleteBtnAction:(UIButton *)sender{

//判断是否选中,选中才能点击

if (_selectState == YES){

//调用代理

[self.delegate btnClick:self andFlag:(int)sender.tag];

}

}

#pragma mark -- 加按钮

/**

*  点击加按钮实现数量的增加

*/

-(void)addBtnAction:(UIButton *)sender{

//判断是否选中,选中才能点击

if (_selectState == YES){

//调用代理

[self.delegate btnClick:self andFlag:(int)sender.tag];

}

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,684评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,143评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,214评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,788评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,796评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,665评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,027评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,679评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,346评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,664评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,766评论 1 331
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,412评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,015评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,974评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,073评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,501评论 2 343

推荐阅读更多精彩内容