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;
//記錄當(dāng)頁面消失時是否需要隱藏系統(tǒng)導(dǎo)航?
?BOOL _isHasTabBarController;
//是否含有tabbar BOOL _isHasNavitationController;
//是否含有導(dǎo)航?
}?
?@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;
?}?
?}?
?//當(dāng)進(jìn)入購物車的時候判斷是否有已選擇的商品,有就清空?
?//主要是提交訂單后再返回到購物車,如果不清空,還會顯示?
?if (self.selectedArray.count > 0){?
?for (RWBGoodsModel *model in self.selectedArray) {?
?model.select = NO;
//這個其實(shí)有點(diǎn)多余,提交訂單后的數(shù)據(jù)源不會包含這些,保險(xiǎn)起見,加上了?
?}?
?[self.selectedArray removeAllObjects];?
?}
?//初始化顯示狀態(tài)?
?_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:@"測試數(shù)據(jù)%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 模仿請求數(shù)據(jù),延遲2s加載數(shù)據(jù),實(shí)際使用時請移除更換 [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;
?}?
}?
?/* * * 計(jì)算已選中商品金額 */
?-(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 - 初始化數(shù)組?
- (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 -- 自定義導(dǎo)航?
- (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];
?//當(dāng)有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; //結(jié)算按鈕 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.backgroundColor = BASECOLOR_RED; btn.frame = CGRectMake(RWBSCREEN_WIDTH - 80, 0, 80, RWBTabBarHeight); [btn setTitle:@"去結(jié)算" forState:UIControlStateNormal]; [btn addTarget:self action:@selector(goToPayButtonClick:) forControlEvents:UIControlEventTouchUpInside]; [backgroundView addSubview:btn]; //合計(jì) 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:@"合計(jì):%@",string]; NSMutableAttributedString *RWBString = [[NSMutableAttributedString alloc]initWithString:text]; NSRange rang = [text rangeOfString:@"合計(jì):"]; [RWBString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:rang]; [RWBString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:rang]; return RWBString; } #pragma mark -- 購物車為空時的默認(rèn)視圖 - (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 { //默認(rèn)視圖背景 UIView *backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, RWBNaigationBarHeight, RWBSCREEN_WIDTH, RWBSCREEN_HEIGHT - RWBNaigationBarHeight)]; backgroundView.tag = TAG_CartEmptyView; [self.view addSubview:backgroundView]; //默認(rèn)圖片 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 { //創(chuàng)建底部視圖 [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]; //判斷已選擇數(shù)組里有無該對象,有就刪除 重新添加 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:@"確定要刪除該商品?刪除后無法恢復(fù)!" 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]) { //從已選中刪除,重新計(jì)算價格 [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]; } //如果刪除的時候數(shù)據(jù)紊亂,可延遲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 -- 頁面按鈕點(diǎn)擊事件 #pragma mark --- 返回按鈕點(diǎn)擊事件 - (void)backButtonClick:(UIButton*)button { if (_isHasNavitationController == NO) { [self dismissViewControllerAnimated:YES completion:nil]; } else { [self.navigationController popViewControllerAnimated:YES]; } } #pragma mark --- 全選按鈕點(diǎn)擊事件 - (void)selectAllBtnClick:(UIButton*)button { button.selected = !button.selected; //點(diǎn)擊全選時,把之前已選擇的全部刪除 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 --- 確認(rèn)選擇,提交訂單按鈕點(diǎn)擊事件 - (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 { // 判斷某個區(qū)的商品是否全選 RWB ShopModel *tempShop = self.dataArray[section]; // 是否全選標(biāo)示符 BOOL isShopAllSelect = YES; for (RWBGoodsModel *model in tempShop.goodsArray) { // 當(dāng)有一個為NO的是時候,將標(biāo)示符置為NO,并跳出循環(huán) 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
//商品數(shù)量
@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;
//數(shù)量
@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 - 按鈕點(diǎn)擊方法
- (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;
? ? //數(shù)量加按鈕
? ? 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];
? ? //數(shù)量顯示
? ? 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;
? ? //數(shù)量減按鈕
? ? 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