美文网首页
MVC架构模式 - iOS

MVC架构模式 - iOS

作者: 快乐的tomato | 来源:发表于2018-12-20 11:05 被阅读19次

What

百度一下:

MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。

Why

为什么要用MVC模式?
好处:

  • MVC设计模式可以说实现了分层开发。各个层都有各个层的作用。
  • 降低了层与层之间的依赖,有利于代码的标准化开发
  • 再用新的代码业务逻辑替换时,只需要替换相对应的层,大大降低了我们的工作量,分工明确。

缺点:

  • 增加了系统结构和实现的复杂性
  • 视图与控制器间的过于紧密的连接
  • 视图对模型数据的低效率访问

这里先忽略缺点,小型的app用这个还是可以的。

How

怎么用?
先看看我以前的用的用法,发现一直是不正宗的,以前用的是
界面


image.png

model
.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface CityModel :  NSObject

@property (nonatomic, copy) NSString *headImage;//
@property (nonatomic, copy) NSString *titleName;//
@property (nonatomic, copy) NSString *className;//
@property (nonatomic, copy) NSString *detailUrl;//

@end

.m

#import "CityModel.h"

@implementation CityModel

@end

view
.h

#import "BaseTableViewCell.h"

@class CityModel;

NS_ASSUME_NONNULL_BEGIN

@interface CityTableViewCell : BaseTableViewCell

@property (nonatomic, strong) UIImageView *headImageView;//头像

@property (nonatomic, strong) UILabel *titleL;//标题

@property (nonatomic, strong) CityModel *cityModel;

@end

NS_ASSUME_NONNULL_END

.m

#import "CityTableViewCell.h"
#import "CityModel.h"

@implementation CityTableViewCell


-(void)createControls{
    
    self.headImageView = [[UIImageView alloc] init];
    self.headImageView.layer.cornerRadius = 15;
    self.headImageView.layer.masksToBounds = YES;
    [self addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(10.0*m6Scale);
        make.centerY.mas_equalTo(self.mas_centerY);
        make.size. mas_equalTo(CGSizeMake(90.0*m6Scale,90.0*m6Scale));
        
    }];
    
    
    self.titleL = [CommonTool createLabelWithTitle:@"" textColor:[UIColor darkGrayColor] textFont:15*m6Scale addSubView:self];
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.headImageView.mas_right).offset(10.0*m6Scale);
        make.centerY.mas_equalTo(self.mas_centerY);
       
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = RGB(231, 234, 237);
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.headImageView.mas_right).offset(0.0*m6Scale);
        make.right.mas_equalTo(self.contentView.mas_right).offset(0.0*m6Scale);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).offset(0.0*m6Scale);
        make.height.mas_equalTo(1);
        
    }];
    
    
    
}

-(void)setCityModel:(CityModel *)cityModel{

    _cityModel = cityModel;
    self.titleL.text = cityModel.titleName;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:cityModel.headImage] placeholderImage:[UIImage imageNamed:@"headImage"]];
    
}

controller

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.dataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CityTableViewCell *cityCell = [CityTableViewCell createCellWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cityCell" WithTableView:tableView];
    
    cityCell.cityModel = self.dataArr[indexPath.row];
    return cityCell;
}

直到我看了这个文章iOS架构入门 - MVC模式实例演示才发现我使用是错误的MVC

相关文章

网友评论

      本文标题:MVC架构模式 - iOS

      本文链接:https://www.haomeiwen.com/subject/efwekqtx.html