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;