開發(fā)從MVC過渡到MVP模式

開發(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)撼泛,自己處理邏輯挠说。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市愿题,隨后出現(xiàn)的幾起案子损俭,更是在濱河造成了極大的恐慌,老刑警劉巖潘酗,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件杆兵,死亡現(xiàn)場離奇詭異,居然都是意外死亡仔夺,警方通過查閱死者的電腦和手機琐脏,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缸兔,“玉大人日裙,你說我怎么就攤上這事≡钐澹” “怎么了阅签?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蝎抽。 經(jīng)常有香客問我政钟,道長路克,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任养交,我火速辦了婚禮精算,結果婚禮上,老公的妹妹穿的比我還像新娘碎连。我一直安慰自己灰羽,他們只是感情好,可當我...
    茶點故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布鱼辙。 她就那樣靜靜地躺著廉嚼,像睡著了一般。 火紅的嫁衣襯著肌膚如雪倒戏。 梳的紋絲不亂的頭發(fā)上怠噪,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天,我揣著相機與錄音杜跷,去河邊找鬼傍念。 笑死,一個胖子當著我的面吹牛葛闷,可吹牛的內容都是我干的憋槐。 我是一名探鬼主播,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼淑趾,長吁一口氣:“原來是場噩夢啊……” “哼阳仔!你這毒婦竟也來了?” 一聲冷哼從身側響起治笨,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤驳概,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后旷赖,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體顺又,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年等孵,在試婚紗的時候發(fā)現(xiàn)自己被綠了稚照。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,834評論 1 347
  • 序言:一個原本活蹦亂跳的男人離奇死亡俯萌,死狀恐怖果录,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情咐熙,我是刑警寧澤弱恒,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站棋恼,受9級特大地震影響返弹,放射性物質發(fā)生泄漏锈玉。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一义起、第九天 我趴在偏房一處隱蔽的房頂上張望拉背。 院中可真熱鬧,春花似錦默终、人聲如沸椅棺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽两疚。三九已至,卻和暖如春含滴,著一層夾襖步出監(jiān)牢的瞬間鬼雀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工蛙吏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鞋吉。 一個月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓鸦做,卻偏偏與公主長得像,于是被迫代替她去往敵國和親谓着。 傳聞我的和親對象是個殘疾皇子泼诱,可洞房花燭夜當晚...
    茶點故事閱讀 44,779評論 2 354