美文网首页
干货!Swift/OC简单的MVVM模式

干货!Swift/OC简单的MVVM模式

作者: 66b6d3e5fc98 | 来源:发表于2017-12-04 21:32 被阅读117次

    MVVM模式,废话不多上代码:

    Swift——基础简易版本

    Swift——RxSwift进阶

    OC——基础简易版本

    1,-Model
    @interface M_NewsList : NSObject
    @property (nullable, nonatomic, strong) NSString* CategoryId;
    @property (nullable, nonatomic, strong) NSString* NewsCommentNum;
    @property (nullable, nonatomic, strong) NSString* NewsId;
    @property (nullable, nonatomic, strong) NSString* NewsSummary;
    @property (nullable, nonatomic, strong) NSString* NewsUrl;
    @property (nullable, nonatomic, strong) NSString* Time;
    @property (nullable, nonatomic, strong) NSString* Title;
    @property (nullable, nonatomic, strong) NSString* Type;
    @property (nullable, nonatomic, strong) NSString* moretitle;
    @end
    
    2,-VM
    @interface VM_NewsList : NSObject
    - (void)getNewsList:(BOOL)refresh;
    
    @property (nonatomic, copy, readonly) __block NSMutableArray *news;
    @property (nonatomic, copy) __block void (^newsListBlock)(void);
    //RAC
    @property (nonatomic, strong, readonly) RACCommand *requestCommand;
    @end
    /**************** .m ********************/
    #import "VM_NewsList.h"
    #import "M_NewsList.h"
    
    @interface VM_NewsList()
    {
        NSInteger page;
    }
    @end
    @implementation VM_NewsList
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            page = 1;
            _news = [NSMutableArray array];
        }
        return self;
    }
    
    - (void)getNewsList:(BOOL)refresh{
        page = refresh ? 1 : page + 1;
        __weak typeof(self) weakSelf = self;
        [My_API getNewsList:@{@"page":[NSString stringWithFormat:@"%ld",(long)page],
                              @"type":@"1",
                              @"processID":@"getNewsList"}
                      block:^(BOOL isOk, id data, NSString *msg) {
                          if (page == 1) {[_news removeAllObjects];};
                          NSLog(@"%@",data);
                          if ([data isKindOfClass:[NSArray class]]) {
                              [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                                  M_NewsList * model = [M_NewsList modelWithDictionary:obj];
                                  [weakSelf.news addObject:model];
                              }];
                          }
                          if (weakSelf.newsListBlock) {
                              weakSelf.newsListBlock();
                          }
                      }];
    }
    
    3,-ViewController
    @interface NewsList ()
    @property (nullable, nonatomic, strong) VM_NewsList* vm_datas;
    @end
    
    @implementation NewsList
    - (VM_NewsList*)vm_datas{
        if (!_vm_datas) {
            _vm_datas = [[VM_NewsList alloc] init];
        }
        return _vm_datas;
    }
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        __weak __typeof(self) weakSelf = self;
        self.vm_datas.newsListBlock = ^{
            [weakSelf.tableView.mj_header endRefreshing];
            [weakSelf.tableView.mj_footer endRefreshing];
            [weakSelf.tableView reloadData];
        };
        [self addRefresh];
    }
    
    - (void)addRefresh{
        __weak typeof(self) weakSelf = self;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf.vm_datas getNewsList:YES];
        }];
        
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf.vm_datas getNewsList:NO];
        }];
        
        [self.tableView.mj_header beginRefreshing];
        self.tableView.mj_footer.automaticallyHidden = YES;
    }
    

    OC——RAC进阶版本

    相关文章

      网友评论

          本文标题:干货!Swift/OC简单的MVVM模式

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