GitHub: https://github.com/lll1024/JVShopcart
效果圖
2017-03-31 10.43.23.gif
ViewController 通常是 iOS 項目中最“重”的文件混卵,它常常會包含很多不必要的、本可以復(fù)用的崩侠、邏輯不清晰的代碼。今天我們將會看到給 ViewController 瘦身的技術(shù)碰声,讓代碼移動到合適的地方變得更加的可復(fù)用撩笆、邏輯性強、耦合性低滑潘。下面以購物車為例進行說明。
把 Data Source 和其他 Protocols 分離出來
不要用 self
作為 tableView
的代理锨咙,而是新建代理類將 UITableViewDataSource
的代碼抽出來放到代理類中语卤,就像下面這個代理類這樣:
#import "JVShopcartTableViewProxy.h"
#import "JVShopcartBrandModel.h"
#import "JVShopcartCell.h"
#import "JVShopcartHeaderView.h"
@implementation JVShopcartTableViewProxy
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
JVShopcartBrandModel *brandModel = self.dataArray[section];
NSArray *productArray = brandModel.products;
return productArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
JVShopcartCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JVShopcartCell"];
JVShopcartBrandModel *brandModel = self.dataArray[indexPath.section];
NSArray *productArray = brandModel.products;
if (productArray.count > indexPath.row) {
JVShopcartProductModel *productModel = productArray[indexPath.row];
NSString *productName = [NSString stringWithFormat:@"%@%@%@", brandModel.brandName, productModel.productStyle, productModel.productType];
NSString *productSize = [NSString stringWithFormat:@"W:%ld H:%ld D:%ld", productModel.specWidth, productModel.specHeight, productModel.specLength];
[cell configureShopcartCellWithProductURL:productModel.productPicUri productName:productName productSize:productSize productPrice:productModel.productPrice productCount:productModel.productQty productStock:productModel.productStocks productSelected:productModel.isSelected];
}
__weak __typeof(self) weakSelf = self;
cell.shopcartCellBlock = ^(BOOL isSelected){
if (weakSelf.shopcartProxyProductSelectBlock) {
weakSelf.shopcartProxyProductSelectBlock(isSelected, indexPath);
}
};
cell.shopcartCellEditBlock = ^(NSInteger count){
if (weakSelf.shopcartProxyChangeCountBlock) {
weakSelf.shopcartProxyChangeCountBlock(count, indexPath);
}
};
return cell;
}
#pragma mark UITableViewDelegate
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
JVShopcartHeaderView *shopcartHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"JVShopcartHeaderView"];
if (self.dataArray.count > section) {
JVShopcartBrandModel *brandModel = self.dataArray[section];
[shopcartHeaderView configureShopcartHeaderViewWithBrandName:brandModel.brandName brandSelect:brandModel.isSelected];
}
__weak __typeof(self) weakSelf = self;
shopcartHeaderView.shopcartHeaderViewBlock = ^(BOOL isSelected){
if (weakSelf.shopcartProxyBrandSelectBlock) {
weakSelf.shopcartProxyBrandSelectBlock(isSelected, section);
}
};
return shopcartHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"刪除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
if (self.shopcartProxyDeleteBlock) {
self.shopcartProxyDeleteBlock(indexPath);
}
}];
UITableViewRowAction *starAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"收藏" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
if (self.shopcartProxyStarBlock) {
self.shopcartProxyStarBlock(indexPath);
}
}];
return @[deleteAction, starAction];
}
@end
用單獨的類處理業(yè)務(wù)邏輯
將所有的業(yè)務(wù)邏輯寫在 ViewController
中是不可取的,這會導(dǎo)致邏輯混亂難以維護的困境酪刀,因為 ViewController
實質(zhì)上擔(dān)負的是個協(xié)調(diào)粹舵、控制和管理的角色。正確的流程應(yīng)該是:View
收到交互事件告訴 ViewController
, 控制器調(diào)用處理類接口并拿到返回結(jié)果再 configure 相應(yīng)的 View
, 就像下面這個控制器這樣:
#import "JVShopcartViewController.h"
#import "JVShopcartTableViewProxy.h"
#import "JVShopcartBottomView.h"
#import "JVShopcartCell.h"
#import "JVShopcartHeaderView.h"
#import "JVShopcartFormat.h"
#import "Masonry.h"
@interface JVShopcartViewController ()<JVShopcartFormatDelegate>
@property (nonatomic, strong) UITableView *shopcartTableView; /**< 購物車列表 */
@property (nonatomic, strong) JVShopcartBottomView *shopcartBottomView; /**< 購物車底部視圖 */
@property (nonatomic, strong) JVShopcartTableViewProxy *shopcartTableViewProxy; /**< tableView代理 */
@property (nonatomic, strong) JVShopcartFormat *shopcartFormat; /**< 負責(zé)購物車邏輯處理 */
@property (nonatomic, strong) UIButton *editButton; /**< 編輯按鈕 */
@end
@implementation JVShopcartViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"購物車";
self.view.backgroundColor = [UIColor whiteColor];
[self addSubview];
[self layoutSubview];
[self requestShopcartListData];
}
- (void)requestShopcartListData {
[self.shopcartFormat requestShopcartProductList];
}
#pragma mark JVShopcartFormatDelegate
//數(shù)據(jù)請求成功回調(diào)
- (void)shopcartFormatRequestProductListDidSuccessWithArray:(NSMutableArray *)dataArray {
self.shopcartTableViewProxy.dataArray = dataArray;
[self.shopcartTableView reloadData];
}
//購物車視圖需要更新時的統(tǒng)一回調(diào)
- (void)shopcartFormatAccountForTotalPrice:(float)totalPrice totalCount:(NSInteger)totalCount isAllSelected:(BOOL)isAllSelected {
[self.shopcartBottomView configureShopcartBottomViewWithTotalPrice:totalPrice totalCount:totalCount isAllselected:isAllSelected];
[self.shopcartTableView reloadData];
}
//點擊結(jié)算按鈕后的回調(diào)
- (void)shopcartFormatSettleForSelectedProducts:(NSArray *)selectedProducts {
}
//批量刪除回調(diào)
- (void)shopcartFormatWillDeleteSelectedProducts:(NSArray *)selectedProducts {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"確認要刪除這%ld個寶貝嗎骂倘?", selectedProducts.count] preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.shopcartFormat deleteSelectedProducts:selectedProducts];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
//全部刪除回調(diào)
- (void)shopcartFormatHasDeleteAllProducts {
}
#pragma mark getters
- (UITableView *)shopcartTableView {
if (_shopcartTableView == nil){
_shopcartTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
_shopcartTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[_shopcartTableView registerClass:[JVShopcartCell class] forCellReuseIdentifier:@"JVShopcartCell"];
[_shopcartTableView registerClass:[JVShopcartHeaderView class] forHeaderFooterViewReuseIdentifier:@"JVShopcartHeaderView"];
_shopcartTableView.showsVerticalScrollIndicator = NO;
_shopcartTableView.delegate = self.shopcartTableViewProxy;
_shopcartTableView.dataSource = self.shopcartTableViewProxy;
_shopcartTableView.rowHeight = 120;
_shopcartTableView.sectionFooterHeight = 10;
_shopcartTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
_shopcartTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
}
return _shopcartTableView;
}
- (JVShopcartTableViewProxy *)shopcartTableViewProxy {
if (_shopcartTableViewProxy == nil){
_shopcartTableViewProxy = [[JVShopcartTableViewProxy alloc] init];
__weak __typeof(self) weakSelf = self;
_shopcartTableViewProxy.shopcartProxyProductSelectBlock = ^(BOOL isSelected, NSIndexPath *indexPath){
[weakSelf.shopcartFormat selectProductAtIndexPath:indexPath isSelected:isSelected];
};
_shopcartTableViewProxy.shopcartProxyBrandSelectBlock = ^(BOOL isSelected, NSInteger section){
[weakSelf.shopcartFormat selectBrandAtSection:section isSelected:isSelected];
};
_shopcartTableViewProxy.shopcartProxyChangeCountBlock = ^(NSInteger count, NSIndexPath *indexPath){
[weakSelf.shopcartFormat changeCountAtIndexPath:indexPath count:count];
};
_shopcartTableViewProxy.shopcartProxyDeleteBlock = ^(NSIndexPath *indexPath){
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"確認要刪除這個寶貝嗎齐婴?" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alert addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[weakSelf.shopcartFormat deleteProductAtIndexPath:indexPath];
}]];
[weakSelf presentViewController:alert animated:YES completion:nil];
};
_shopcartTableViewProxy.shopcartProxyStarBlock = ^(NSIndexPath *indexPath){
[weakSelf.shopcartFormat starProductAtIndexPath:indexPath];
};
}
return _shopcartTableViewProxy;
}
- (JVShopcartBottomView *)shopcartBottomView {
if (_shopcartBottomView == nil){
_shopcartBottomView = [[JVShopcartBottomView alloc] init];
__weak __typeof(self) weakSelf = self;
_shopcartBottomView.shopcartBotttomViewAllSelectBlock = ^(BOOL isSelected){
[weakSelf.shopcartFormat selectAllProductWithStatus:isSelected];
};
_shopcartBottomView.shopcartBotttomViewSettleBlock = ^(){
[weakSelf.shopcartFormat settleSelectedProducts];
};
_shopcartBottomView.shopcartBotttomViewStarBlock = ^(){
[weakSelf.shopcartFormat starSelectedProducts];
};
_shopcartBottomView.shopcartBotttomViewDeleteBlock = ^(){
[weakSelf.shopcartFormat beginToDeleteSelectedProducts];
};
}
return _shopcartBottomView;
}
- (JVShopcartFormat *)shopcartFormat {
if (_shopcartFormat == nil){
_shopcartFormat = [[JVShopcartFormat alloc] init];
_shopcartFormat.delegate = self;
}
return _shopcartFormat;
}
- (UIButton *)editButton {
if (_editButton == nil){
_editButton = [UIButton buttonWithType:UIButtonTypeCustom];
_editButton.frame = CGRectMake(0, 0, 40, 40);
[_editButton setTitle:@"編輯" forState:UIControlStateNormal];
[_editButton setTitle:@"完成" forState:UIControlStateSelected];
[_editButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
_editButton.titleLabel.font = [UIFont systemFontOfSize:13];
[_editButton addTarget:self action:@selector(editButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _editButton;
}
- (void)editButtonAction {
self.editButton.selected = !self.editButton.isSelected;
[self.shopcartBottomView changeShopcartBottomViewWithStatus:self.editButton.isSelected];
}
- (void)addSubview {
UIBarButtonItem *editBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.editButton];
self.navigationItem.rightBarButtonItem = editBarButtonItem;
[self.view addSubview:self.shopcartTableView];
[self.view addSubview:self.shopcartBottomView];
}
- (void)layoutSubview {
[self.shopcartTableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.right.equalTo(self.view);
make.bottom.equalTo(self.shopcartBottomView.mas_top);
}];
[self.shopcartBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(self.view);
make.height.equalTo(@50);
}];
}
@end
處理類接口如下:
#import <Foundation/Foundation.h>
@protocol JVShopcartFormatDelegate <NSObject>
@required
- (void)shopcartFormatRequestProductListDidSuccessWithArray:(NSMutableArray *)dataArray;
- (void)shopcartFormatAccountForTotalPrice:(float)totalPrice
totalCount:(NSInteger)totalCount
isAllSelected:(BOOL)isAllSelected;
- (void)shopcartFormatSettleForSelectedProducts:(NSArray *)selectedProducts;
- (void)shopcartFormatWillDeleteSelectedProducts:(NSArray *)selectedProducts;
- (void)shopcartFormatHasDeleteAllProducts;
@end
@interface JVShopcartFormat : NSObject
@property (nonatomic, weak) id <JVShopcartFormatDelegate> delegate;
//請求購物車數(shù)據(jù)
- (void)requestShopcartProductList;
//選中/取消選中某個row
- (void)selectProductAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected;
//選中/取消選中某個section
- (void)selectBrandAtSection:(NSInteger)section isSelected:(BOOL)isSelected;
//改變商品數(shù)
- (void)changeCountAtIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count;
//單個刪除商品
- (void)deleteProductAtIndexPath:(NSIndexPath *)indexPath;
//批量刪除商品
- (void)beginToDeleteSelectedProducts;
- (void)deleteSelectedProducts:(NSArray *)selectedArray;
//單個收藏商品
- (void)starProductAtIndexPath:(NSIndexPath *)indexPath;
//批量收藏商品
- (void)starSelectedProducts;
//全選 or 取消全選
- (void)selectAllProductWithStatus:(BOOL)isSelected;
//結(jié)算選中商品
- (void)settleSelectedProducts;
@end
購物車的組成:
-
JVShopcartViewController
: 購物車控制器 負責(zé)協(xié)調(diào)Model和View 只有100多行代碼 -
JVShopcartFormat
: 負責(zé)網(wǎng)絡(luò)請求與邏輯處理 -
JVShopcartTableViewProxy
: 作為控制器里邊TableView的代理 -
View
: 包括Cell、HeaderView稠茂、CountView(改變商品數(shù)的視圖)柠偶、BottomView(控制器底部包含結(jié)算按鈕的視圖) -
Model
: 包含BrandModel和ProductModel兩層
- (void)shopcartFormatRequestProductListDidSuccessWithArray:(NSMutableArray *)dataArray;
- (void)shopcartFormatAccountForTotalPrice:(float)totalPrice
totalCount:(NSInteger)totalCount
isAllSelected:(BOOL)isAllSelected;
- (void)shopcartFormatSettleForSelectedProducts:(NSArray *)selectedProducts;
- (void)shopcartFormatHasDeleteAllProducts;
- 這是請求購物車列表成功之后的回調(diào)方法,將裝有Model的數(shù)組回調(diào)到控制器睬关;控制器將其賦給TableView的代理類
JVShopcartTableViewProxy
并刷新TableView诱担。 - 這是用戶在操作了單選、多選电爹、全選蔫仙、刪除這些會改變底部結(jié)算視圖里邊的全選按鈕狀態(tài)、商品總價和商品數(shù)的統(tǒng)一回調(diào)方法丐箩,這條API會將用戶操作之后的結(jié)果摇邦,也就是是否全選、商品總價和和商品總數(shù)回調(diào)給
JVShopcartViewController
屎勘, 控制器拿著這些數(shù)據(jù)調(diào)用底部結(jié)算視圖BottomView的configure方法并刷新TableView施籍,就完成了UI更新。 - 這是用戶點擊結(jié)算按鈕的回調(diào)方法概漱,這條API會將剔除了未選中ProductModel的模型數(shù)組回調(diào)給
JVShopcartViewController
丑慎,但并不改變原數(shù)據(jù)源因為用戶隨時可能返回。 - 這是用戶刪除了購物車所有數(shù)據(jù)之后的回調(diào)方法,你可能會做些視圖的隱藏或者提示竿裂。
關(guān)于JVShopcartFormat
玉吁,這個類主要負責(zé)網(wǎng)絡(luò)請求與邏輯處理以及結(jié)果的回調(diào)。下面依次介紹這些方法:
- (void)requestShopcartProductList;
- (void)selectProductAtIndexPath:(NSIndexPath *)indexPath isSelected:(BOOL)isSelected;
- (void)selectBrandAtSection:(NSInteger)section isSelected:(BOOL)isSelected;
- (void)changeCountAtIndexPath:(NSIndexPath *)indexPath count:(NSInteger)count;
- (void)deleteProductAtIndexPath:(NSIndexPath *)indexPath;
- (void)starProductAtIndexPath:(NSIndexPath *)indexPath;
- (void)selectAllProductWithStatus:(BOOL)isSelected;
- (void)settleSelectedProducts;
- 這是請求購物車數(shù)據(jù)源的方法腻异,大家一般都是對AFNetworking進行二次封裝來請求數(shù)據(jù)进副。
- 這是用戶選中了某個產(chǎn)品或某個row的處理方法,因為這會改變底部結(jié)算視圖所以一定會回調(diào)上文協(xié)議中的第二個方法悔常, 下同影斑。
- 這是用戶選中了某個品牌或某個section的處理方法
- 這是用戶改變了商品數(shù)量的處理方法
- 這是用戶刪除操作的處理方法
- 這是用戶收藏操作的處理方法,這里沒有回調(diào)任何方法这嚣,也可以根據(jù)需求添加回調(diào)方法鸥昏。
- 這是用戶全選操作的處理方法
- 這是用戶結(jié)算操作的處理方法