iOS-MVC架构优化
Posted 淡然微笑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-MVC架构优化相关的知识,希望对你有一定的参考价值。
MVC 架构问题:
用户代理,业务逻辑,UI ,内部方法,代码封装导致:
①VC代码过于繁重(封装)
②代码耦合性过高(解耦)
1.TableView优化之封装(初始化方法和代理方法封装)
HKDataSource.h
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef void(^CellConfigure)(id cell , id model, NSIndexPath *indexPath); @interface HKDataSource : NSObject<UITableViewDelegate,UITableViewDataSource> @property (nonatomic, strong) NSMutableArray * dataArray ; //自定义初始化方法 - (id)initWithIdentifier:(NSString *)identifier configureBlock:(CellConfigure)configure; //CellIdentifier @property (nonatomic, strong) IBInspectable NSString * cellIdentifier ; //cellConfigure @property (nonatomic, copy) CellConfigure cellConfigure ; - (void)addDataArray:(NSArray*)array; - (id)modelsAtIndexPath:(NSIndexPath*)indexPath; @end
HKDataSource.h
#import "HKDataSource.h" @implementation HKDataSource - (id)initWithIdentifier:(NSString *)identifier configureBlock:(CellConfigure)configure { if (self = [super init]) { _cellIdentifier = identifier; _cellConfigure = [configure copy]; } return self; } -(void)addDataArray:(NSArray *)array { if (!array) { return; } if (self.dataArray.count>0) { [self.dataArray removeAllObjects]; } [self.dataArray addObjectsFromArray:array]; } - (id)modelsAtIndexPath:(NSIndexPath*)indexPath { return self.dataArray.count > indexPath.row ? self.dataArray[indexPath.row] : nil; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return !self.dataArray ? 0 : self.dataArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath]; id model = [self modelsAtIndexPath:indexPath]; if (self.cellConfigure) { self.cellConfigure(cell, model, indexPath); } return cell; } @end
使用:
static NSString * reuseId = @"reuseId"; @interface ViewController () @property (nonatomic, strong) UITableView * tableView; @property (nonatomic, strong) NSMutableArray * dataArray ; @property (nonatomic, strong) HKDataSource * dataSource ; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.dataSource = [[HKDataSource alloc] initWithIdentifier:reuseId configureBlock:^(MVCTableViewCell *cell, Model *model, NSIndexPath *indexPath) { cell.model = model; }]; [self loadData]; [self.dataSource addDataArray:self.dataArray]; self.view.backgroundColor = [UIColor whiteColor]; [self.view addSubview:self.tableView]; self.tableView.dataSource = self.dataSource; } - (void)loadData { NSArray * tempArray = @[@{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"}, @{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"}, @{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"}, @{@"name":@"Hank",@"imageUrl":@"http://cc",@"num":@"99"}]; for (int i = 0; i<tempArray.count; i++) { Model * model = [[Model alloc] init]; [model setValuesForKeysWithDictionary:tempArray[i]]; [self.dataArray addObject:model]; } } -(UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.tableFooterView = [UIView new]; _tableView.backgroundColor = [UIColor whiteColor]; [_tableView registerClass:[MVCTableViewCell class] forCellReuseIdentifier:reuseId]; } return _tableView; } @end
2.Cell耦合性强(解耦)
MVCTableViewCell * cell
cell.model = _dataArray[indexPath.row].
cell 内部(setModel:+UI赋值)[耦合性太强…]
3.cell复用 UI改变了->model不变(双向绑定)
self.model.num = self.numLabel.text;
以上是关于iOS-MVC架构优化的主要内容,如果未能解决你的问题,请参考以下文章
使用 C++ 反转句子中的每个单词需要对我的代码片段进行代码优化
Android 逆向整体加固脱壳 ( DEX 优化流程分析 | DexPrepare.cpp 中 dvmOptimizeDexFile() 方法分析 | /bin/dexopt 源码分析 )(代码片段