電商項目購物車

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]}];

//初始化數(shù)據(jù)

allPrice = 0.0;

infoArr = [[NSMutableArray alloc]init];

/**

*? 初始化一個數(shù)組,數(shù)組里面放字典蔓钟。字典里面放的是單元格需要展示的數(shù)據(jù)

*/

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

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

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

[infoDict setValue:@"這是商品標(biāo)題" forKey:@"goodsTitle"];

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

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

[infoDict setValue:[NSNumber numberWithInt:1] forKey:@"goodsNum"];

//封裝數(shù)據(jù)模型

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

//將數(shù)據(jù)模型放入數(shù)組中

[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)];

//添加一個全選文本框標(biāo)簽

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:@"復(fù)選框-未選中"] forState:UIControlStateNormal];

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

[footView addSubview:_allSelectBtn];

//添加小結(jié)文本框

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

lab2.textColor = [UIColor redColor];

lab2.text = @"小結(jié):";

[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];

//添加一個結(jié)算按鈕

UIButton *settlementBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];

[settlementBtn setTitle:@"去結(jié)算" 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;

}

//定制單元格內(nèi)容

- (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;

}

//調(diào)用方法百匆,給單元格賦值

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

return cell;

}

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

return 120;

}

//單元格選中事件

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

/**

*? 判斷當(dāng)期是否為選中狀態(tài)漾稀,如果選中狀態(tài)點擊則更改成未選中芝加,如果未選中點擊則更改成選中狀態(tài)

*/

GoodsInfoModel *model = infoArr[indexPath.row];

if (model.selectState){

model.selectState = NO;

}else{

model.selectState = YES;

}

//刷新整個表格

//[_MyTableView reloadData];

//刷新當(dāng)前行

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

[self totalPrice];

}

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

{

NSIndexPath *index = [_MyTableView indexPathForCell:cell];

switch (flag) {

case 11:

{

//做減法

//先獲取到當(dāng)期行數(shù)據(jù)源內(nèi)容耍攘,改變數(shù)據(jù)源內(nèi)容武通,刷新表格

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];

}

//全選按鈕的選中狀態(tài)

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

//判斷是否選中奉呛,是改成否计螺,否改成是,改變圖片狀態(tài)

sender.tag = !sender.tag;

if (sender.tag){

[sender setImage:[UIImage imageNamed:@"復(fù)選框-選中.png"]? forState:UIControlStateNormal];

}else{

[sender setImage:[UIImage imageNamed:@"復(fù)選框-未選中.png"] forState:UIControlStateNormal];

}

//改變單元格選中狀態(tài)

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瞧壮,因為每次的都是全部循環(huán)算一遍

allPrice = 0.0;

}





#import@interface GoodsInfoModel : NSObject

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

@property(strong,nonatomic)NSString *goodsTitle;//商品標(biāo)題

@property(strong,nonatomic)NSString *goodsPrice;//商品單價

@property(assign,nonatomic)BOOL selectState;//是否選中狀態(tài)

@property(assign,nonatomic)int goodsNum;//商品個數(shù)

-(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"http://添加代理登馒,用于按鈕加減的實現(xiàn)@protocol MyCustomCellDelegate-(void)btnClick:(UITableViewCell *)cell andFlag:(int)flag;@end@interface MyCustomCell : UITableViewCell@property(strong,nonatomic)UIImageView *goodsImgV;//商品圖片@property(strong,nonatomic)UILabel *goodsTitleLab;//商品標(biāo)題@property(strong,nonatomic)UILabel *priceTitleLab;//價格標(biāo)簽@property(strong,nonatomic)UILabel *priceLab;//具體價格@property(strong,nonatomic)UILabel *goodsNumLab;//購買數(shù)量標(biāo)簽@property(strong,nonatomic)UILabel *numCountLab;//購買商品的數(shù)量@property(strong,nonatomic)UIButton *addBtn;//添加商品數(shù)量@property(strong,nonatomic)UIButton *deleteBtn;//刪除商品數(shù)量@property(strong,nonatomic)UIButton *isSelectBtn;//是否選中按鈕@property(strong,nonatomic)UIImageView *isSelectImg;//是否選中圖片@property(assign,nonatomic)BOOL selectState;//選中狀態(tài)@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];

//添加商品標(biāo)題

_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];

//購買數(shù)量

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

_goodsNumLab.text = @"購買數(shù)量:";

[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];

//購買商品的數(shù)量

_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 里面存放各個控件需要的數(shù)值

*/

-(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:@"復(fù)選框-選中"];

}else{

_selectState = NO;

_isSelectImg.image = [UIImage imageNamed:@"復(fù)選框-未選中"];

}

}

#pragma mark -- 減按鈕

/**

*? 點擊減按鈕實現(xiàn)數(shù)量的減少

*/

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

//判斷是否選中,選中才能點擊

if (_selectState == YES){

//調(diào)用代理

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

}

}

#pragma mark -- 加按鈕

/**

*? 點擊加按鈕實現(xiàn)數(shù)量的增加

*/

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

//判斷是否選中咆槽,選中才能點擊

if (_selectState == YES){

//調(diào)用代理

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

}

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末陈轿,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子秦忿,更是在濱河造成了極大的恐慌麦射,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件灯谣,死亡現(xiàn)場離奇詭異法褥,居然都是意外死亡,警方通過查閱死者的電腦和手機酬屉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門半等,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人呐萨,你說我怎么就攤上這事杀饵。” “怎么了谬擦?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵切距,是天一觀的道長。 經(jīng)常有香客問我惨远,道長谜悟,這世上最難降的妖魔是什么话肖? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮葡幸,結(jié)果婚禮上最筒,老公的妹妹穿的比我還像新娘。我一直安慰自己蔚叨,他們只是感情好床蜘,可當(dāng)我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著蔑水,像睡著了一般邢锯。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上搀别,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天丹擎,我揣著相機與錄音,去河邊找鬼歇父。 笑死鸥鹉,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的庶骄。 我是一名探鬼主播毁渗,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼单刁!你這毒婦竟也來了灸异?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤羔飞,失蹤者是張志新(化名)和其女友劉穎肺樟,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體逻淌,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡么伯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了卡儒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片田柔。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖骨望,靈堂內(nèi)的尸體忽然破棺而出硬爆,到底是詐尸還是另有隱情,我是刑警寧澤擎鸠,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布缀磕,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏袜蚕。R本人自食惡果不足惜糟把,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牲剃。 院中可真熱鬧遣疯,春花似錦、人聲如沸颠黎。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽狭归。三九已至,卻和暖如春文判,著一層夾襖步出監(jiān)牢的瞬間过椎,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工戏仓, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留疚宇,地道東北人。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓赏殃,卻偏偏與公主長得像敷待,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子仁热,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容