MVVM
来源:互联网 发布:罗大佑 演唱会 知乎 编辑:程序博客网 时间:2024/05/02 02:37
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);
}];
}
MovieViewModel *viewModel = [[MovieViewModelalloc]init];
viewModel.returnBlock = ^(id returnValue){
tableView.dataSource = returnValue;
[tableView reloadData];
};
viewModel.errorBlock = ^(id errorCode){
NSLog(@"%@",errorCode);
};
[viewModel getMovieData];
//
// 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:framecollectionViewLayout: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
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- mvvm
- MVVM
- MVVM
- mvvm
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- MVVM
- "undefined reference to" 问题解决方法
- android:inputType常用取值
- Oracle中dual表的用途介绍
- java单例模式详解完美实现(包括反射破坏的防止和线程安全)
- Linux发展史
- MVVM
- 【PHP】如何提高网页加载速度?
- 深度学习21天实战Caffe学习笔记--笔记5--caffe中前向传播、反向传播及最优化求解过程
- 视频摘要和视频浓缩的区别
- poj-3258-River Hopscotch
- 即时通讯(IM)
- 关于Linux中CP命令用法的一些冷知识
- Python:发送电子邮件
- 命名