关于猿题库MVVM without binding with d

2018-08-20  本文已影响62人  西门淋雨

先借用一张图,大致的逻辑如下:

image

新建项目,结构如下:

BFD8DE24-C84B-47D6-A943-C70FEEA99F62.png

各个类的代码如下:

DYBaseDataVC

#import <Foundation/Foundation.h>
typedef void (^DYDataResponseBlock)(id response,NSError *error);
@interface DYBaseDataVC : NSObject

@end
#import "DYBaseDataVC.h"

@implementation DYBaseDataVC

@end

HomeModel

#import <Foundation/Foundation.h>

@interface HomeModel : NSObject
@property (nonatomic,strong)NSString *name;
@property (nonatomic,assign)NSInteger age;
@property (nonatomic,strong)NSString *address;
@end
#import "HomeModel.h"

@implementation HomeModel

@end

ViewController

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end
#import "ViewController.h"
#import "CustomView.h"
#import "ViewControllerDatacontroller.h"
#import "HomeViewModel.h"
@interface ViewController ()
@property (nonatomic,strong) CustomView *userInfoView;
@property (nonatomic,strong) ViewControllerDatacontroller *dataController;

@end

@implementation ViewController
#pragma -mark lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化ui
    [self setUI];
    //获取数据,模拟网络获取数据
    [self getData];
}
#pragma -mark initUI
- (void)setUI{
    self.userInfoView.frame = CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 70);
    [self.view addSubview:self.userInfoView];
}
#pragma -mark Data
- (void)getData{
    [self.dataController getNetWorkDataWithBlock:^(id response, NSError *error) {
        if (error) {
            return;
        }
        //组装viewModel数据
        HomeViewModel *model = [HomeViewModel viewModelWith:(HomeModel *)response];
        //刷新ui
        [self.userInfoView bindData:model];
    }];
}
#pragma mark - lazy
- (CustomView *)userInfoView{
    if (nil == _userInfoView) {
        _userInfoView = [[CustomView alloc] init];
    }
    return _userInfoView;
}
- (ViewControllerDatacontroller *)dataController{
    if (nil == _dataController) {
        _dataController = [[ViewControllerDatacontroller alloc] init];
    }
    return _dataController;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end

ViewControllerDatacontroller

#import <Foundation/Foundation.h>
#import "DYBaseDataVC.h"
#import "HomeModel.h"
@interface ViewControllerDatacontroller : DYBaseDataVC
//获取数据
- (void)getNetWorkDataWithBlock:(DYDataResponseBlock)block;
@end
#import "ViewControllerDatacontroller.h"

@implementation ViewControllerDatacontroller
- (void)getNetWorkDataWithBlock:(DYDataResponseBlock)block{
    //模拟网络数据...
    HomeModel *model = [[HomeModel alloc] init];
    model.address = @"北京";
    model.age = 20;
    model.name = @"张三";
    if (block) {
        block(model,nil);
    }
}
@end

CustomView

#import <UIKit/UIKit.h>
#import "HomeViewModel.h"
@interface CustomView : UIView
//根据viewModel组装的数据,显示ui
- (void)bindData:(HomeViewModel *)model;
@end
#import "CustomView.h"
@interface CustomView()
@property (nonatomic,strong) UILabel *nameLa;
@property (nonatomic,strong) UILabel *ageLa;
@property (nonatomic,strong) UILabel *addressLa;
@property (nonatomic,strong) HomeViewModel *model;

@end
@implementation CustomView

- (instancetype) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
}
- (void)bindData:(HomeViewModel *)model{
    if (nil == model) {
        return;
    }
    self.model = model;
    self.backgroundColor = self.model.bgColor;
    
    self.nameLa.frame = CGRectMake(30, 0, CGRectGetWidth(self.frame), 20);
    self.nameLa.text = self.model.model.name;
    [self addSubview:self.nameLa];
    self.ageLa.frame = CGRectMake(30, CGRectGetMaxY(self.nameLa.frame)+5, CGRectGetWidth(self.frame), 20);
    self.ageLa.text = [NSString stringWithFormat:@"%ld",(long)self.model.model.age];
    [self addSubview:self.ageLa];

    self.addressLa.frame = CGRectMake(30, CGRectGetMaxY(self.ageLa.frame)+5, CGRectGetWidth(self.frame), 20);
    self.addressLa.text = self.model.model.address;
    [self addSubview:self.addressLa];

}
#pragma -mark lazy
- (UILabel *)nameLa{
    if (nil == _nameLa) {
        _nameLa = [[UILabel alloc] init];
        _nameLa.backgroundColor = [UIColor clearColor];
        _nameLa.font = self.model.titleFont;
        _nameLa.textAlignment = NSTextAlignmentLeft;
        _nameLa.textColor = [UIColor redColor];
    }
    return _nameLa;
}
- (UILabel *)ageLa{
    if (nil == _ageLa) {
        _ageLa = [[UILabel alloc] init];
        _ageLa.backgroundColor = [UIColor clearColor];
        _ageLa.font = self.model.titleFont;
        _ageLa.textAlignment = NSTextAlignmentLeft;
        _ageLa.textColor = [UIColor redColor];

    }
    return _ageLa;
}
- (UILabel *)addressLa{
    if (nil == _addressLa) {
        _addressLa = [[UILabel alloc] init];
        _addressLa.backgroundColor = [UIColor clearColor];
        _addressLa.font = self.model.titleFont;
        _addressLa.textAlignment = NSTextAlignmentLeft;
        _addressLa.textColor = [UIColor redColor];

    }
    return _addressLa;
}
@end

HomeViewModel

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "HomeModel.h"

@interface HomeViewModel : NSObject
//model
@property (nonatomic,strong)HomeModel *model;
//背景颜色
@property (nonatomic,strong)UIColor *bgColor;
//字体大小
@property (nonatomic,strong)UIFont *titleFont;

+ (HomeViewModel *)viewModelWith:(HomeModel *)homeModel;
@end
#import "HomeViewModel.h"

@implementation HomeViewModel
+ (HomeViewModel *)viewModelWith:(HomeModel *)homeModel{
    //组装view需要的数据
    HomeViewModel *model = [[HomeViewModel alloc] init];
    model.model = homeModel;
    model.bgColor = [UIColor lightGrayColor];
    model.titleFont = [UIFont systemFontOfSize:10];
    return model;
}
@end

功能很简单,只是实现了获取数据后,UI的刷新展示。不知道理解的是否有问题,有问题欢迎提出。
Demo git 链接地址

上一篇 下一篇

猜你喜欢

热点阅读