AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *vc=[[ViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.backgroundColor=[UIColor whiteColor];
self.window.rootViewController=nav;
[self.window makeKeyAndVisible];
return YES;
}
ViewController.m
#import "ViewController.h"
#import "RWBCartViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (IBAction)toShoppingCart:(id)sender {
RWBCarViewController *rwbCar=[[RWBCarViewController alloc]init];
[self.navigationController pushViewController:rwbCar animated:YES];
}
RWBCartViewController.m
#import "RWBCartViewController.h"
#import "RWBConfigFile.h"
#import "RWBCartTableViewCell.h"
#import "RWBShopModel.h"
#import "RWBGoodsModel.h"
#import "RWBTableHeaderView.h"
@interface RWBCartViewController () {
BOOL _isHiddenNavigationBarWhenDisappear;
//记录当页面消失时是否需要隐藏系统导航
BOOL _isHasTabBarController;
//是否含有tabbar BOOL _isHasNavitationController;
//是否含有导航
}
@property (strong,nonatomic)NSMutableArray *dataArray;
@property (strong,nonatomic)NSMutableArray *selectedArray;
@property (strong,nonatomic)UITableView *myTableView;
@property (strong,nonatomic)UIButton *allSellectedButton;
@property (strong,nonatomic)UILabel *totlePriceLabel;
@end
@implementation DWQCartViewController
#pragma mark - viewController life cicle
- (void)viewWillAppear:(BOOL)animated {
if (_isHasNavitationController == YES) {
if (self.navigationController.navigationBarHidden == YES) {
_isHiddenNavigationBarWhenDisappear = NO;
}
else {
self.navigationController.navigationBarHidden = YES;
_isHiddenNavigationBarWhenDisappear = YES;
}
}
//当进入购物车的时候判断是否有已选择的商品,有就清空
//主要是提交订单后再返回到购物车,如果不清空,还会显示
if (self.selectedArray.count > 0){
for (RWBGoodsModel *model in self.selectedArray) {
model.select = NO;
//这个其实有点多余,提交订单后的数据源不会包含这些,保险起见,加上了
}
[self.selectedArray removeAllObjects];
}
//初始化显示状态
_allSellectedButton.selected = NO;
_totlePriceLabel.attributedText = [self DWQSetString:@"¥0.00"];
}
-(void)creatData {
// for (int i = 0; i < 10; i++) {
//RWB CartModel *model = [[RWBCartModel alloc]init];
// // model.title = [NSString stringWithFormat:@"测试数据%d",i];
// model.price = @"100.00"; // model.number = 1;
// model.image = [UIImage imageNamed:@"aaa.jpg"];
// model.dateStr = @"2016.02.18";
// model.subTitle = @"18*20cm";
// // [self.dataArray addObject:model];
//
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"ShopCarSources" ofType:@"plist" inDirectory:nil];
NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:path];
NSLog(@"%@",dic); NSArray *array = [dic objectForKey:@"data"];
if (array.count > 0) {
for (NSDictionary *dic in array) {
RWB ShopModel *model = [[RWBShopModel alloc]init];
model.shopID = [dic objectForKey:@"id"];
model.shopName = [dic objectForKey:@"shopName"];
model.sID = [dic objectForKey:@"sid"];
[model configGoodsArrayWithArray:[dic objectForKey:@"items"]];
[self.dataArray addObject:model];
}
}
// if (self.myTableView != nil) {
//
[self.myTableView reloadData];
//
}
else {
// [self setupCartView];
//
}
}
- (void)loadData {
[self creatData];
[self changeView];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view. self.view.backgroundColor = [UIColor whiteColor];
_isHasTabBarController = self.tabBarController?YES:NO;
_isHasNavitationController = self.navigationController?YES:NO;
#warning 模仿请求数据,延迟2s加载数据,实际使用时请移除更换 [self performSelector:@selector(loadData) withObject:nil afterDelay:2];
[self setupCustomNavigationBar];
if (self.dataArray.count > 0) {
[self setupCartView];
}
else {
[self setupCartEmptyView];
}
}
- (void)viewWillDisappear:(BOOL)animated {
if (_isHiddenNavigationBarWhenDisappear == YES) {
self.navigationController.navigationBarHidden = NO;
}
}
/* * * 计算已选中商品金额 */
-(void)countPrice {
double totlePrice = 0.0;
for (DWQGoodsModel *model in self.selectedArray) {
double price = [model.price doubleValue];
totlePrice += price * model.count;
}
NSString *string = [NSString stringWithFormat:@"¥%.2f",totlePrice];
self.totlePriceLabel.attributedText = [self RWBSetString:string];
}
#pragma mark - 初始化数组
- (NSMutableArray *)dataArray {
if (_dataArray == nil) {
_dataArray = [NSMutableArray arrayWithCapacity:0];
}
return _dataArray;
}
- (NSMutableArray *)selectedArray {
if (_selectedArray == nil) {
_selectedArray = [NSMutableArray arrayWithCapacity:0];
}
return _selectedArray;
}
#pragma mark - 布局页面视图
#pragma mark -- 自定义导航
- (void)setupCustomNavigationBar {
UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, RWBSCREEN_WIDTH, RWBNaigationBarHeight)];
backgroundView.backgroundColor = RWBColorFromRGB(236, 236, 236);
[self.view addSubview:backgroundView];
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, RWBNaigationBarHeight - 0.5, RWBSCREEN_WIDTH, 0.5)];
lineView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:lineView];
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.text = @"购物车";
titleLabel.font = [UIFont systemFontOfSize:20];
titleLabel.center = CGPointMake(self.view.center.x, (RWBNaigationBarHeight - 20)/2.0 + 20);
CGSize size = [titleLabel sizeThatFits:CGSizeMake(300, 44)];
titleLabel.bounds = CGRectMake(0, 0, size.width + 20, size.height);
titleLabel.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:titleLabel];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 20, 40, 44);
[backButton setImage:[UIImage imageNamed:dwq_BackButtonString] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backButton];
}
#pragma mark -- 自定义底部视图
- (void)setupCustomBottomView {
UIView *backgroundView = [[UIView alloc]init];
backgroundView.backgroundColor = DWQColorFromRGB(245, 245, 245);
backgroundView.tag = TAG_CartEmptyView + 1;
[self.view addSubview:backgroundView];
//当有tabBarController时,在tabBar的上面
if (_isHasTabBarController == YES) {
backgroundView.frame = CGRectMake(0, RWBSCREEN_HEIGHT - 2*RWBTabBarHeight, RWBSCREEN_WIDTH, RWBTabBarHeight);
}
else {
backgroundView.frame = CGRectMake(0, RWBSCREEN_HEIGHT - RWBTabBarHeight, RWBSCREEN_WIDTH, RWBTabBarHeight);
}
UIView *lineView = [[UIView alloc]init];
lineView.frame = CGRectMake(0, 0, RWBSCREEN_WIDTH, 1);
lineView.backgroundColor = [UIColor lightGrayColor]; [backgroundView addSubview:lineView]; //全选按钮 UIButton *selectAll = [UIButton buttonWithType:UIButtonTypeCustom]; selectAll.titleLabel.font = [UIFont systemFontOfSize:16]; selectAll.frame = CGRectMake(10, 5, 80, RWBTabBarHeight - 10); [selectAll setTitle:@" 全选" forState:UIControlStateNormal]; [selectAll setImage:[UIImage imageNamed:dwq_Bottom_UnSelectButtonString] forState:UIControlStateNormal]; [selectAll setImage:[UIImage imageNamed:dwq_Bottom_SelectButtonString] forState:UIControlStateSelected]; [selectAll setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [selectAll addTarget:self action:@selector(selectAllBtnClick:) forControlEvents:UIControlEventTouchUpInside]; [backgroundView addSubview:selectAll]; self.allSellectedButton = selectAll; //结算按钮 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.backgroundColor = BASECOLOR_RED; btn.frame = CGRectMake(RWBSCREEN_WIDTH - 80, 0, 80, RWBTabBarHeight); [btn setTitle:@"去结算" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(goToPayButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [backgroundView addSubview:btn]; //合计 UILabel *label = [[UILabel alloc]init]; label.font = [UIFont systemFontOfSize:16]; label.textColor = [UIColor redColor]; [backgroundView addSubview:label]; label.attributedText = [self RWBSetString:@"¥0.00"]; CGFloat maxWidth = RWBSCREEN_WIDTH - selectAll.bounds.size.width - btn.bounds.size.width - 30; // CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, RWBTabBarHeight)]; label.frame = CGRectMake(selectAll.bounds.size.width + 20, 0, maxWidth - 10, DWQTabBarHeight); self.totlePriceLabel = label; } - (NSMutableAttributedString*)RWBSetString:(NSString*)string { NSString *text = [NSString stringWithFormat:@"合计:%@",string]; NSMutableAttributedString *RWBString = [[NSMutableAttributedString alloc]initWithString:text]; NSRange rang = [text rangeOfString:@"合计:"]; [RWBString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:rang]; [RWBString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:rang]; return RWBString; } #pragma mark -- 购物车为空时的默认视图 - (void)changeView { if (self.dataArray.count > 0) { UIView *view = [self.view viewWithTag:TAG_CartEmptyView]; if (view != nil) { [view removeFromSuperview]; } [self setupCartView]; } else { UIView *bottomView = [self.view viewWithTag:TAG_CartEmptyView + 1]; [bottomView removeFromSuperview]; [self.myTableView removeFromSuperview]; self.myTableView = nil; [self setupCartEmptyView]; } } - (void)setupCartEmptyView { //默认视图背景 UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, RWBNaigationBarHeight, RWBSCREEN_WIDTH, RWBSCREEN_HEIGHT - RWBNaigationBarHeight)]; backgroundView.tag = TAG_CartEmptyView; [self.view addSubview:backgroundView]; //默认图片 UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:rwb_CartEmptyString]]; img.center = CGPointMake(RWBSCREEN_WIDTH/2.0, RWBSCREEN_HEIGHT/2.0 - 120); img.bounds = CGRectMake(0, 0, 247.0/187 * 100, 100); [backgroundView addSubview:img]; UILabel *warnLabel = [[UILabel alloc]init]; warnLabel.center = CGPointMake(RWBSCREEN_WIDTH/2.0, DWQSCREEN_HEIGHT/2.0 - 10); warnLabel.bounds = CGRectMake(0, 0, RWBSCREEN_WIDTH, 30); warnLabel.textAlignment = NSTextAlignmentCenter; warnLabel.text = @"购物车为空!"; warnLabel.font = [UIFont systemFontOfSize:15]; warnLabel.textColor = RWBColorFromHex(0x706F6F); [backgroundView addSubview:warnLabel]; } #pragma mark -- 购物车有商品时的视图 - (void)setupCartView { //创建底部视图 [self setupCustomBottomView]; UITableView *table = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped]; table.delegate = self; table.dataSource = self; table.rowHeight = dwq_CartRowHeight; table.separatorStyle = UITableViewCellSeparatorStyleNone; table.backgroundColor = RWBColorFromRGB(245, 246, 248); [self.view addSubview:table]; self.myTableView = table; if (_isHasTabBarController) { table.frame = CGRectMake(0, RWBNaigationBarHeight, RWBSCREEN_WIDTH, RWBSCREEN_HEIGHT - RWBNaigationBarHeight - 2*RWBTabBarHeight); } else { table.frame = CGRectMake(0, RWBNaigationBarHeight, RWBSCREEN_WIDTH, RWBSCREEN_HEIGHT - RWBNaigationBarHeight - RWBTabBarHeight); } [table registerClass:[RWBTableHeaderView class] forHeaderFooterViewReuseIdentifier:@"RWBHeaderView"]; } #pragma mark --- UITableViewDataSource & UITableViewDelegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { RWB ShopModel *model = [self.dataArray objectAtIndex:section]; return model.goodsArray.count; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RWB CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RWBCartReusableCell"]; if (cell == nil) { cell = [[RWBCartTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"RWBCartReusableCell"]; } RWB ShopModel *shopModel = self.dataArray[indexPath.section]; RWB GoodsModel *model = [shopModel.goodsArray objectAtIndex:indexPath.row]; __block typeof(cell)wsCell = cell; [cell numberAddWithBlock:^(NSInteger number) { wsCell.rwbNumber = number; model.count = number; [shopModel.goodsArray replaceObjectAtIndex:indexPath.row withObject:model]; if ([self.selectedArray containsObject:model]) { [self.selectedArray removeObject:model]; [self.selectedArray addObject:model]; [self countPrice]; } }]; [cell numberCutWithBlock:^(NSInteger number) { wsCell.dwqNumber = number; model.count = number; [shopModel.goodsArray replaceObjectAtIndex:indexPath.row withObject:model]; //判断已选择数组里有无该对象,有就删除 重新添加 if ([self.selectedArray containsObject:model]) { [self.selectedArray removeObject:model]; [self.selectedArray addObject:model]; [self countPrice]; } }]; [cell cellSelectedWithBlock:^(BOOL select) { model.select = select; if (select) { [self.selectedArray addObject:model]; } else { [self.selectedArray removeObject:model]; } [self verityAllSelectState]; [self verityGroupSelectState:indexPath.section]; [self countPrice]; }]; [cell reloadDataWithModel:model]; return cell; } -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath { return @"删除"; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {RWB TableHeaderView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"RWBHeaderView"]; RWB ShopModel *model = [self.dataArray objectAtIndex:section]; NSLog(@">>>>>>%d", model.select); view.title = model.shopName; view.select = model.select; view.rwbClickBlock = ^(BOOL select) { model.select = select; if (select) { for RWBGoodsModel *good in model.goodsArray) { good.select = YES; if (![self.selectedArray containsObject:good]) { [self.selectedArray addObject:good]; } } } else { for (DWQGoodsModel *good in model.goodsArray) { good.select = NO; if ([self.selectedArray containsObject:good]) { [self.selectedArray removeObject:good]; } } } [self verityAllSelectState]; [tableView reloadData]; [self countPrice]; }; return view; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return RWBTableViewHeaderHeight; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 1; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要删除该商品?删除后无法恢复!" preferredStyle:1]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { RWBShopModel *shop = [self.dataArray objectAtIndex:indexPath.section]; RWBGoodsModel *model = [shop.goodsArray objectAtIndex:indexPath.row]; [shop.goodsArray removeObjectAtIndex:indexPath.row]; // 删除 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; if (shop.goodsArray.count == 0) { [self.dataArray removeObjectAtIndex:indexPath.section]; } //判断删除的商品是否已选择 if ([self.selectedArray containsObject:model]) { //从已选中删除,重新计算价格 [self.selectedArray removeObject:model]; [self countPrice]; } NSInteger count = 0; for (DWQShopModel *shop in self.dataArray) { count += shop.goodsArray.count; } if (self.selectedArray.count == count) { _allSellectedButton.selected = YES; } else { _allSellectedButton.selected = NO; } if (count == 0) { [self changeView]; } //如果删除的时候数据紊乱,可延迟0.5s刷新一下 [self performSelector:@selector(reloadTable) withObject:nil afterDelay:0.5]; }]; UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:okAction]; [alert addAction:cancel]; [self presentViewController:alert animated:YES completion:nil]; } } - (void)reloadTable { [self.myTableView reloadData]; } #pragma mark -- 页面按钮点击事件 #pragma mark --- 返回按钮点击事件 - (void)backButtonClick:(UIButton*)button { if (_isHasNavitationController == NO) { [self dismissViewControllerAnimated:YES completion:nil]; } else { [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark --- 全选按钮点击事件 - (void)selectAllBtnClick:(UIButton*)button { button.selected = !button.selected; //点击全选时,把之前已选择的全部删除 for (RWBGoodsModel *model in self.selectedArray) { model.select = NO; } [self.selectedArray removeAllObjects]; if (button.selected) { for (RWBShopModel *shop in self.dataArray) { shop.select = YES; for (RWBGoodsModel *model in shop.goodsArray) { model.select = YES; [self.selectedArray addObject:model]; } } } else { for (RWBShopModel *shop in self.dataArray) { shop.select = NO; } } [self.myTableView reloadData]; [self countPrice]; } #pragma mark --- 确认选择,提交订单按钮点击事件 - (void)goToPayButtonClick:(UIButton*)button { if (self.selectedArray.count > 0) { for (RWBGoodsModel *model in self.selectedArray) { NSLog(@"选择的商品>>%@>>>%ld",model,(long)model.count); } } else { NSLog(@"你还没有选择任何商品"); } } - (void)verityGroupSelectState:(NSInteger)section { // 判断某个区的商品是否全选 RWB ShopModel *tempShop = self.dataArray[section]; // 是否全选标示符 BOOL isShopAllSelect = YES; for (RWBGoodsModel *model in tempShop.goodsArray) { // 当有一个为NO的是时候,将标示符置为NO,并跳出循环 if (model.select == NO) { isShopAllSelect = NO; break; } } RWBTableHeaderView *header = (RWBTableHeaderView *)[self.myTableView headerViewForSection:section]; header.select = isShopAllSelect; tempShop.select = isShopAllSelect; } - (void)verityAllSelectState { NSInteger count = 0; for (RWBShopModel *shop in self.dataArray) { count += shop.goodsArray.count; } if (self.selectedArray.count == count) { _allSellectedButton.selected = YES; } else { _allSellectedButton.selected = NO; } }
RWBCartTableViewCell.h
#import@class RWBGoodsModel;
typedef void(^RWBNumberChangedBlock)(NSInteger number);
typedef void(^RWBCellSelectedBlock)(BOOL select);
@interface RWBCartTableViewCell : UITableViewCell
//商品数量
@property (assign,nonatomic)NSInteger rwbNumber;
@property (assign,nonatomic)BOOL rwbSelected;
- (void)reloadDataWithModel:(RWBGoodsModel*)model;
- (void)numberAddWithBlock:(RWBNumberChangedBlock)block;
- (void)numberCutWithBlock:(RWBNumberChangedBlock)block;
- (void)cellSelectedWithBlock:(RWBCellSelectedBlock)block;
@end
RWBCartTableViewCell.m
#import "RWBCartTableViewCell.h"
#import "RWBConfigFile.h"
//#import "RWBCartModel.h"
#import "RWBGoodsModel.h"
@interface RWBCartTableViewCell ()
{
RWBNumberChangedBlock numberAddBlock;
RWBNumberChangedBlock numberCutBlock;
RWBCellSelectedBlock cellSelectedBlock;
}
//选中按钮
@property (nonatomic,retain) UIButton *selectBtn;
//显示照片
@property (nonatomic,retain) UIImageView *rwbImageView;
//商品名
@property (nonatomic,retain) UILabel *nameLabel;
//尺寸
@property (nonatomic,retain) UILabel *sizeLabel;
//时间
@property (nonatomic,retain) UILabel *dateLabel;
//价格
@property (nonatomic,retain) UILabel *priceLabel;
//数量
@property (nonatomic,retain)UILabel *numberLabel;
@end
@implementation RWBCartTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = RWBColorFromRGB(245, 246, 248);
self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setupMainView];
}
return self;
}
#pragma mark - public method
- (void)reloadDataWithModel:(RWBGoodsModel*)model {
self.dwqImageView.image = model.image;
self.nameLabel.text = model.goodsName;
self.priceLabel.text = model.price;
self.dateLabel.text = model.price;
self.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)model.count];
// self.sizeLabel.text = model.sizeStr;
self.selectBtn.selected = model.select;
}
- (void)numberAddWithBlock:(RWBNumberChangedBlock)block {
numberAddBlock = block;
}
- (void)numberCutWithBlock:(RWBNumberChangedBlock)block {
numberCutBlock = block;
}
- (void)cellSelectedWithBlock:(RWBCellSelectedBlock)block {
cellSelectedBlock = block;
}
#pragma mark - 重写setter方法
- (void)setDwqNumber:(NSInteger)rwbNumber {
_rwbNumber = rwbNumber;
self.numberLabel.text = [NSString stringWithFormat:@"%ld",(long)rwbNumber];
}
- (void)setRwbSelected:(BOOL)rwbSelected {
_rwbSelected = rwbSelected;
self.selectBtn.selected = rwbSelected;
}
#pragma mark - 按钮点击方法
- (void)selectBtnClick:(UIButton*)button {
button.selected = !button.selected;
if (cellSelectedBlock) {
cellSelectedBlock(button.selected);
}
}
- (void)addBtnClick:(UIButton*)button {
NSInteger count = [self.numberLabel.text integerValue];
count++;
if (numberAddBlock) {
numberAddBlock(count);
}
}
- (void)cutBtnClick:(UIButton*)button {
NSInteger count = [self.numberLabel.text integerValue];
count--;
if(count <= 0){
return ;
}
if (numberCutBlock) {
numberCutBlock(count);
}
}
#pragma mark - 布局主视图
-(void)setupMainView {
//白色背景
UIView *bgView = [[UIView alloc]init];
bgView.frame = CGRectMake(10, 10, RWBSCREEN_WIDTH - 20, rwb_CartRowHeight - 10);
bgView.backgroundColor = [UIColor whiteColor];
bgView.layer.borderColor = RWBColorFromHex(0xEEEEEE).CGColor;
bgView.layer.borderWidth = 1;
[self addSubview:bgView];
//选中按钮
UIButton* selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
selectBtn.center = CGPointMake(20, bgView.height/2.0);
selectBtn.bounds = CGRectMake(0, 0, 30, 30);
[selectBtn setImage:[UIImage imageNamed:rwb_Bottom_UnSelectButtonString] forState:UIControlStateNormal];
[selectBtn setImage:[UIImage imageNamed:rwb_Bottom_SelectButtonString] forState:UIControlStateSelected];
[selectBtn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:selectBtn];
self.selectBtn = selectBtn;
//照片背景
UIView *imageBgView = [[UIView alloc]init];
imageBgView.frame = CGRectMake(selectBtn.right + 5, 5, bgView.height - 10, bgView.height - 10);
imageBgView.backgroundColor = RWBColorFromHex(0xF3F3F3);
[bgView addSubview:imageBgView];
//显示照片
UIImageView* imageView = [[UIImageView alloc]init];
imageView.image = [UIImage imageNamed:@"default_pic_1"];
imageView.frame = CGRectMake(imageBgView.left + 5, imageBgView.top + 5, imageBgView.width - 10, imageBgView.height - 10);
imageView.contentMode = UIViewContentModeScaleAspectFit;
[bgView addSubview:imageView];
self.dwqImageView = imageView;
CGFloat width = (bgView.width - imageBgView.right - 30)/2.0;
//价格
UILabel* priceLabel = [[UILabel alloc]init];
priceLabel.frame = CGRectMake(bgView.width - width - 10, 10, width, 30);
priceLabel.font = [UIFont boldSystemFontOfSize:16];
priceLabel.textColor = BASECOLOR_RED;
priceLabel.textAlignment = NSTextAlignmentRight;
[bgView addSubview:priceLabel];
self.priceLabel = priceLabel;
//商品名
UILabel* nameLabel = [[UILabel alloc]init];
nameLabel.frame = CGRectMake(imageBgView.right + 10, 10, width, 25);
nameLabel.font = [UIFont systemFontOfSize:15];
[bgView addSubview:nameLabel];
self.nameLabel = nameLabel;
//尺寸
UILabel* sizeLabel = [[UILabel alloc]init];
sizeLabel.frame = CGRectMake(nameLabel.left, nameLabel.bottom + 5, width, 20);
sizeLabel.textColor = RWBColorFromRGB(132, 132, 132);
sizeLabel.font = [UIFont systemFontOfSize:12];
[bgView addSubview:sizeLabel];
self.sizeLabel = sizeLabel;
//时间
UILabel* dateLabel = [[UILabel alloc]init];
dateLabel.frame = CGRectMake(nameLabel.left, sizeLabel.bottom , width, 20);
dateLabel.font = [UIFont systemFontOfSize:10];
dateLabel.textColor = RWBColorFromRGB(132, 132, 132);
[bgView addSubview:dateLabel];
self.dateLabel = dateLabel;
//数量加按钮
UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
addBtn.frame = CGRectMake(bgView.width - 35, bgView.height - 35, 25, 25);
[addBtn setImage:[UIImage imageNamed:@"cart_addBtn_nomal"] forState:UIControlStateNormal];
[addBtn setImage:[UIImage imageNamed:@"cart_addBtn_highlight"] forState:UIControlStateHighlighted];
[addBtn addTarget:self action:@selector(addBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:addBtn];
//数量显示
UILabel* numberLabel = [[UILabel alloc]init];
numberLabel.frame = CGRectMake(addBtn.left - 30, addBtn.top, 30, 25);
numberLabel.textAlignment = NSTextAlignmentCenter;
numberLabel.text = @"1";
numberLabel.font = [UIFont systemFontOfSize:15];
[bgView addSubview:numberLabel];
self.numberLabel = numberLabel;
//数量减按钮
UIButton *cutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
cutBtn.frame = CGRectMake(numberLabel.left - 25, addBtn.top, 25, 25);
[cutBtn setImage:[UIImage imageNamed:@"cart_cutBtn_nomal"] forState:UIControlStateNormal];
[cutBtn setImage:[UIImage imageNamed:@"cart_cutBtn_highlight"] forState:UIControlStateHighlighted];
[cutBtn addTarget:self action:@selector(cutBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[bgView addSubview:cutBtn];
}
@end
RWBTableHeaderView.h
#importtypedef void(^buttonClickBlock)(BOOL select);
@interface RWBTableHeaderView : UITableViewHeaderFooterView
@property (copy,nonatomic)NSString *title;
@property (copy,nonatomic)buttonClickBlock rwbClickBlock;
@property (assign,nonatomic)BOOL select;
@end
RWBTableHeaderView.m
#import "RWBTableHeaderView.h"
#import "RWBConfigFile.h"
@interface RWBTableHeaderView ()
@property (strong,nonatomic)UILabel *titleLabel;
@property (strong,nonatomic)UIButton *button;
@end
@implementation RWBTableHeaderView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
[self setupUI];
}
return self;
}
- (void)setupUI {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(5, 15, 50, 30);
[button setImage:[UIImage imageNamed:@"cart_unSelect_btn"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"cart_selected_btn"] forState:UIControlStateSelected];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:button];
self.button = button;
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(70, 15, RWBSCREEN_WIDTH - 100, 30);
label.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:label];
self.titleLabel = label;
}
- (void)buttonClick:(UIButton*)button {
button.selected = !button.selected;
if (self.rwbClickBlock) {
self.rwbClickBlock(button.selected);
}
}
- (void)setSelect:(BOOL)select {
self.button.selected = select;
_select = select;
}
- (void)setTitle:(NSString *)title {
self.titleLabel.text = title;
_title = title;
}
RWBGoodsModel.h
#import #import @interface RWBGoodsModel : NSObject @property (nonatomic,assign) BOOL select; @property (assign,nonatomic)NSInteger count; @property (copy,nonatomic)NSString *goodsID; @property (copy,nonatomic)NSString *goodsName; @property (copy,nonatomic)NSString *price; @property (strong,nonatomic)UIImage *image; @end
RWBShopModel.h
#import @interface RWBShopModel : NSObject @property (assign,nonatomic)BOOL select; @property (copy,nonatomic)NSString *shopID; @property (copy,nonatomic)NSString *shopName; @property (copy,nonatomic)NSString *sID; @property (strong,nonatomic,readonly)NSMutableArray *goodsArray; - (void)configGoodsArrayWithArray:(NSArray*)array; @end
RWBShopModel.m
#import "RWBShopModel.h"
#import "RWBGoodsModel.h"
@implementation RWBShopModel
- (void)configGoodsArrayWithArray:(NSArray*)array; {
if (array.count > 0) {
NSMutableArray *dataArray = [NSMutableArray arrayWithCapacity:0];
for (NSDictionary *dic in array) {
RWBGoodsModel *model = [[RWBGoodsModel alloc]init];
model.count = [[dic objectForKey:@"count"] integerValue];
model.goodsID = [dic objectForKey:@"goodsId"];
model.goodsName = [dic objectForKey:@"goodsName"];
model.price = [NSString stringWithFormat:@"%@",[dic objectForKey:@"realPrice"]];
if ([dic objectForKey:@"goodsPic"]) {
model.image=[UIImage imageNamed:[dic objectForKey:@"goodsPic"]];
}
[dataArray addObject:model];
}
_goodsArray = [dataArray mutableCopy];
}
}
@end