開發(fā)從MVC過渡到MVP模式
iOS開發(fā)中梭冠,我們用的最多就是mvc模式開發(fā)了蓝角,下面這行代碼大家在熟悉不過了吧
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",self.identifier);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier ];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.identifier];
}
//同過重寫set方法
cell.model = model
return cell;
}
但是由于 cell.model = model這句代碼會導致耦合度很高疲憋,也就是不能用以下這種場景開發(fā)3807682-1949a0691ea89601.jpeg
我們希望用以下mvc模式開發(fā)龙巨,model與view不產(chǎn)生聯(lián)系
3807682-0a753ed103230c8f.jpeg
MVP模式開發(fā)
mvp模式開發(fā)就是一種很好的解耦方式,我們直接上代碼霜浴。
//將tableview封裝起來晶衷,通過block將cell,model回調
+(instancetype)createTableViewWithDatas:(NSArray *)datas indentifier:(NSString *)indentifier cellModelBlock:(CellModelBlock)cellModelBlock selectIndexPath:(SelectIndexPath)selectIndexPath
{
return [[[self class]alloc]initWithDatas:datas indentifier:indentifier cellModelBlock:cellModelBlock selectIndexPath:selectIndexPath];
}
-(instancetype)initWithDatas:(NSArray *)datas indentifier:(NSString *)indentifier cellModelBlock:(CellModelBlock)cellModelBlock selectIndexPath:(SelectIndexPath)selectIndexPath
{
if (self = [super init]) {
self.datas = datas;
self.cellModelBlock = [cellModelBlock copy];
self.selectIndexPath = [selectIndexPath copy];
self.identifier = indentifier;
}
return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.datas.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",self.identifier);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.identifier ];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:self.identifier];
}
if (self.cellModelBlock) {
self.cellModelBlock(cell, self.datas[indexPath.row], indexPath);
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.selectIndexPath) {
self.selectIndexPath(indexPath);
}
}
// tableviwCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self addUI];
}
return self;
}
-(void)addUI{
//姓名
_nameLabel = [[UILabel alloc]init];
_nameLabel.text = @"g";
[_nameLabel setTextColor:[UIColor blackColor]];
_nameLabel.frame = CGRectMake(0, 0, 100, 40);
[self.contentView addSubview:_nameLabel];
//數(shù)字
_numLabel = [[UILabel alloc]init];
_numLabel.text = @"10";
[_numLabel setTextColor:[UIColor blackColor]];
_numLabel.frame = CGRectMake(100, 0, 100, 40);
[self.contentView addSubview:_numLabel];
//自增按鈕
_addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_addButton setTitle:@"增加" forState:UIControlStateNormal];
_addButton.frame = CGRectMake(200, 0, 50, 40);
_addButton.backgroundColor = [UIColor grayColor];
[self.contentView addSubview:_addButton];
[_addButton addTarget:self action:@selector(addNum) forControlEvents:UIControlEventTouchUpInside];
}
//傳統(tǒng)mvc將在這里改變model阴孟,mvp這里通過代理,讓數(shù)據(jù)層去改變model税迷,也是就說view層不去管理數(shù)據(jù)層的東西
-(void)addNum{
self.num ++;
self.numLabel.text = [NSString stringWithFormat:@"%d",self.num];
if (self.delegate && [self.delegate respondsToSelector:@selector(addNumDelegate:indexPath:)]) {
[self.delegate addNumDelegate:self.num indexPath:self.indexPath_1];
}
}
mvp實現(xiàn)關鍵工具類
//需要改變的數(shù)據(jù)直接通過代理永丝,在這里修改數(shù)據(jù)源
-(instancetype)init
{
if (self = [super init]) {
NSArray *tempArray = @[
@{@"name":@"ggg",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"hhhhhh",@"num":@"100"},
@{@"name":@"zzzzxxxxx",@"num":@"100"},
@{@"name":@"ggg",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"zzzz",@"num":@"100"},
@{@"name":@"hhhhhh",@"num":@"100"},
@{@"name":@"zzzzxxxxx",@"num":@"100"},
];
NSMutableArray *temp = [[NSMutableArray alloc]init];
for (NSDictionary *dict in tempArray) {
Model *model = [[Model alloc]init];
model.name = dict[@"name"];
model.num = dict[@"num"];
[temp addObject:model];
}
self.datas = temp;
}
return self;
}
-(void)addNumDelegate:(int)num indexPath:(NSIndexPath *)indexPath
{
Model *model = self.datas[indexPath.row];
model.num = [NSString stringWithFormat:@"%d",num];
NSLog(@"%@",self.datas);
}
控制器實現(xiàn)方法
self.protocolt = [[protocolTool alloc]init];
self.tableViewTools = [TableViewTableTools createTableViewWithDatas:self.protocolt.datas indentifier:@"cell" cellModelBlock:^(testTableViewCell * cell,Model * model, NSIndexPath * _Nonnull indexPath) {
cell.nameLabel.text = model.name; ;
cell.numLabel.text = model.num;
cell.delegate = self.protocolt;
cell.indexPath_1 = indexPath;
} selectIndexPath:^(NSIndexPath * _Nonnull indexPath) {
NSLog(@"%@",indexPath);
}];
self.tableview.delegate = self.tableViewTools;
self.tableview.dataSource = self.tableViewTools;
通過控制器可以看出,比傳統(tǒng)mvc更加簡潔箭养,vc里面僅僅處理頁面邏輯慕嚷,復雜的東西都交給別去做,自己僅僅需要幾行代碼就能實現(xiàn)需求毕泌。這就是mvp模式開發(fā)的好處喝检,同理另外一個MVVM模式也是類型道理,將vc的大量代理提取到一個類去實現(xiàn)撼泛,自己處理邏輯挠说。