MVVM

MVVM是Model-View-ViewModel的简写。,MVVM就是MVC的扩展,把数据处理全放到ViewModel里进行处理


一个模块文件中,分别有ViewController,View,Model,ViewModel四个文件  其中控制器VC在ViewController里,重写的view放到View文件夹里,数据模型放到Model里

数据请求等一系列操作放到ViewModel里



首先在ViewModel里写一个MovieModel类


#import <Foundation/Foundation.h>

#import "MovieModel.h"

#import <UIKit/UIKit.h>


typedef void (^ReturnValueBlock) (id returnValue);

typedef void (^ErrorCodeBlock) (id errorCode);


@interface MovieViewModel : NSObject


@property (nonatomic,copy)ReturnValueBlock returnBlock;

@property (nonatomic,copy)ErrorCodeBlock errorBlock;


//获取电影数据

- (void)getMovieData;


//跳转到电影详情页

- (void)movieDetailWithPublicModel: (MovieModel *)movieModel WithViewController: (UIViewController *)superController;

@end



#import "MovieViewModel.h"

#import "NetworkService.h"

#import "MovieModel.h"

#import "MovieViewController.h"


@implementation MovieViewModel


- (void)getMovieData{

    [NetworkServicerequestWithURL:@"/v2/movie/coming_soon"params:nilsuccess:^(id result) {

        NSLog(@"%@",result);

        

        NSArray *subjects = result[@"subjects"];

        NSMutableArray *modelArr = [NSMutableArrayarrayWithCapacity:subjects.count];

        for (NSDictionary *subjectin subjects) {

            MovieModel *model = [[MovieModelalloc]init];

            model.movieName = subject[@"title"];

            model.year = subject[@"year"];

            NSString *urlStr = subject[@"images"][@"medium"];

            model.imageUrl = [NSURLURLWithString:urlStr];

            model.detailUrl = subject[@"alt"];

            [modelArr addObject:model];

        }

        _returnBlock(modelArr);

        

    } failure:^(NSError *error) {

         NSLog(@"%@",error);

        _errorBlock(error);

    }];

}




然后在VC调用的时候(可以写成类方法)

  MovieViewModel *viewModel = [[MovieViewModelalloc]init];

    viewModel.returnBlock = ^(id returnValue){

        

        tableView.dataSource = returnValue;

        [tableView reloadData];

    };

    viewModel.errorBlock = ^(id errorCode){

      

        NSLog(@"%@",errorCode);

    };

    

    [viewModel getMovieData];

    

也可以重写tableView,把一系列的tableView的代理方法写到本身里


一下是我自己写的一个collectionView 在VIewModel请求数据,在VC回调block,可以在block里直接把数据赋值,直接给collectionView.dataSource,然后reload

//

//  MeiziCollectionView.m

//  meizi

//

//  Created by 俊洋洋 on 16/7/8.

//  Copyright © 2016俊洋洋. All rights reserved.

//


#import "MeiziCollectionView.h"

#import "meiziCollectionViewCell.h"

@interface MeiziCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>


@end



@implementation MeiziCollectionView


- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout

{

    self = [superinitWithFrame:frame collectionViewLayout:layout];

    self.backgroundColor =[UIColorwhiteColor];

    self.dataSource =self;

    self.delegate =self;

    [selfregisterClass:[meiziCollectionViewCellclass] forCellWithReuseIdentifier:@"cellID"];

    return self;

}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    returnself.dataSourcea.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    meiziCollectionViewCell *cell = [meiziCollectionViewCellcellWithCollectionView:collectionViewAtIndexPath:indexPath];

    [cell setupUI];

    cell.backgroundColor = [UIColorcyanColor];

    cell.model = self.dataSourcea[indexPath.row];

    return cell;

}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    returnCGSizeMake(SCREENWIDTH/2.0f-10,SCREENWIDTH/2.0f-10);

}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{

    returnUIEdgeInsetsMake(15,5, 0,5);

//    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)

}

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/


@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值