MVP-架构分析
MVC跟MVP的区别以及使用的优缺点?
MVC:View和控制器耦合性强
MVP:面向协议,View和Model完全解耦,获取数据(网络遵循协议拿数据,Controller层不显示网络请求数据的过程,只要遵循协议就能拿到数据)
UserPresenter层
#import <Foundation/Foundation.h> #import "User.h" @interface UserViewData : NSObject /** 名字 */ @property (nonatomic, strong) NSString * name ; /** 年龄 */ @property (nonatomic, strong) NSString * age ; - (instancetype)initWithName:(NSString*)name age:(NSString*)age; @end NS_ASSUME_NONNULL_BEGIN @protocol UserViewDelegate <NSObject> - (void)startLoading; - (void)finishLoading; //用户界面的数据 - (void)setUsers:(NSArray<UserViewData *> *)userViewData; //数据是空的时候显示 - (void)setEmptyUsers; @end @interface UserPresenter : NSObject @property (nonatomic, weak) id<UserViewDelegate> delegate ; - (void)getUsers; @end NS_ASSUME_NONNULL_END #import "UserPresenter.h" @implementation UserViewData - (instancetype)initWithName:(NSString*)name age:(NSString*)age { self = [super init]; if (self) { self.name = name; self.age = age; } return self; } @end @implementation UserPresenter -(void)getUsers { __weak typeof(self) weakSelf = self; [User getUsers:^(NSArray<User *> * _Nonnull users) { [weakSelf.delegate finishLoading]; BOOL hasUser = users && users.count; if (!hasUser) { [weakSelf.delegate setEmptyUsers]; }else { NSMutableArray<UserViewData *> *userViewDatas = [NSMutableArray arrayWithCapacity:11]; for (User * user in users) { NSString * ageStr = [NSString stringWithFormat:@"年龄%zd",user.age]; UserViewData * userViewData = [[UserViewData alloc] initWithName:user.firstName age:ageStr]; [userViewDatas addObject:userViewData]; } if ([self.delegate respondsToSelector:@selector(setUsers:)]) { [self.delegate setUsers:userViewDatas]; } } }]; } @end
使用:
#import "ViewController.h" #import "User.h" #import "UserPresenter.h" @interface ViewController ()<UITableViewDataSource,UserViewDelegate> /** TableView */ @property (nonatomic, strong) UITableView * tableView ; /** 指示器 */ @property (nonatomic, strong) UIActivityIndicatorView * indicator ; /** 空白占位 */ @property (nonatomic, strong) UIView * emptyView ; /** Protocol */ @property (nonatomic, strong) UserPresenter * userPresenter ; /** 数据源 */ @property (nonatomic, strong) NSArray<UserViewData*> * userViewDatas ; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self setupUI]; [self loadData]; } - (void)setupUI { _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; [self.view addSubview:_tableView]; _tableView.dataSource = self; } - (void)loadData { self.userPresenter = [[UserPresenter alloc] init]; self.userPresenter.delegate = self; [self.userPresenter getUsers]; } #pragma mark - UserViewDelegate -(void)startLoading { self.indicator.hidden = NO; [self.indicator startAnimating]; } - (void)finishLoading { [self.indicator stopAnimating]; self.indicator.hidden = YES; } //用户界面的数据 -(void)setUsers:(NSArray<UserViewData *> *)userViewData { self.userViewDatas = userViewData; self.tableView.hidden = NO; self.emptyView.hidden = YES; [self.tableView reloadData]; } //数据是空的时候显示 -(void)setEmptyUsers { self.emptyView.hidden = NO; self.tableView.hidden = YES; self.indicator.hidden = YES; } #pragma mark - 数据源方法 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.userViewDatas.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString * cellID=@"cellID"; UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID]; } UserViewData *userViewData = self.userViewDatas[indexPath.row]; cell.textLabel.text = userViewData.name; cell.detailTextLabel.text = userViewData.age; return cell; } @end