Ios 项目从头开发 MVVM模式(三)
1.话说,本来想做个聚合查询功能。可是我的重点想研究xmpp聊天功能。所以使用mvvm模式做了全然模式51job主界面的页面。
2.首先给大家看我执行起来的界面。
3.界面非常easy,做这个界面主要是为了比較mvvm模式和mvc模式之间的差别。
4.这个界面的结构是下边这张图片
与mvc相比,我多了一个viewmodel文件。
mvc之前是把业务逻辑和数据放在viewcontroller里边,逻辑复杂的话,别人维护起来非常麻烦。
我就不贴viewcontroller的图片了,我把这个代码上传给大家,大家能够看看,和mvc相比,是不是非常easy维护。代码层级会好一些。明天開始研究iosxmpp的聊天功能,所以会暂停一段时间更新。
没办法,看来仅仅能贴代码了,我仅仅贴viewcontroller和viewmodel的代码。大家能够比較下。
这是viewcontroller
#import <UIKit/UIKit.h>
@class MTSOnlineViewModel;
@interface MTSOnlineViewController :UITableViewController
@property(strong,nonatomic)MTSOnlineViewModel *onlineViewModel;
@end
#import "MTSOnlineViewController.h"
#import "MTSOnlineViewModel.h"
#import "MTSOnlineMenuCell.h"
@interface MTSOnlineViewController()<MTSOnlineMenuDelegate>
@end
@implementation MTSOnlineViewController
#pragma mark - UIViewController Overrides
- (void)awakeFromNib
{
[superawakeFromNib];
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.onlineViewModel=[[MTSOnlineViewModelalloc] init];
[self.tableViewsetRowHeight:130.0f];
[self.tableViewsetSeparatorStyle:UITableViewCellSeparatorStyleNone];
@weakify(self);
[self.onlineViewModel.updatedContentSignalsubscribeNext:^(id x) {
@strongify(self);
[self.tableViewreloadData];
}];
}
-(void)viewWillAppear:(BOOL)animated {
[superviewWillAppear:animated];
self.onlineViewModel.active =YES;
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.onlineViewModelnumberOfItems];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MTSOnlineMenuCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"onlinecell"forIndexPath:indexPath];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.delegate=self;
[cell configureCell:self.onlineViewModel.tableDataSourceindex:indexPath.row+1];
return cell;
}
#pragma mark --cell delegate
-(void)pressMenuButton:(MTSMenuType)type title:(NSString*)title;
{
[[[UIAlertViewalloc] initWithTitle:@"button測试"message:title delegate:nilcancelButtonTitle:@"确认"otherButtonTitles:nil,nil] show];
}
@end
这是viewmodel
#import "RVMViewModel.h"
@interface MTSOnlineViewModel :RVMViewModel
@property (nonatomic,readonly) RACSignal *updatedContentSignal;
@property (nonatomic,readonly) NSMutableArray *tableDataSource;
-(NSInteger)numberOfItems;
@end
#import "MTSOnlineViewModel.h"
#import "MTSMenuModel.h"
@interface MTSOnlineViewModel ()
@property (nonatomic,strong) RACSubject *updatedContentSignal;
@property (nonatomic,strong) NSMutableArray *tableDataSource;
@end
@implementation MTSOnlineViewModel
-(instancetype)init {
self = [superinit];
if (self ==nil) returnnil;
self.updatedContentSignal = [[RACSubjectsubject] setNameWithFormat:@"MTSOnlineViewModel updatedContentSignal"];
self.tableDataSource = [[NSMutableArrayalloc] init];
@weakify(self)
[self.didBecomeActiveSignalsubscribeNext:^(id x) {
@strongify(self);
[selfmenuDataSource];
}];
return self;
}
#pragma mark - Public Methods
-(NSInteger)numberOfItems{
NSUInteger count = [self.tableDataSourcecount]/3;
return [self.tableDataSourcecount]%3==0?count:count+1;
}
-(void)menuDataSource{
[self.tableDataSourceaddObject:[[MTSMenuModelalloc] init:@"职位搜索"imagePath:@"jobsearch.png"imagePressPath:@"jobsearch_press.png"type:JobSearch]];
[self.tableDataSourceaddObject:[[MTSMenuModelalloc] init:@"校园招聘"imagePath:@"campus.png"imagePressPath:@"campus_press.png"type:Campus]];
[self.tableDataSourceaddObject:[[MTSMenuModelalloc] init:@"职场资讯"imagePath:@"worknews.png"imagePressPath:@"worknews_press.png"type:WorkNews]];
[self.tableDataSourceaddObject:[[MTSMenuModelalloc] init:@"企业粉丝团"imagePath:@"fans.png"imagePressPath:@"fans_focus.png"type:Fans]];
[self.tableDataSourceaddObject:[[MTSMenuModelalloc] init:@"My 51job"imagePath:@"my51job.png"imagePressPath:@"my51job_focus.png"type:My51Job]];
[self.tableDataSourceaddObject:[[MTSMenuModelalloc] init:@"简历中心"imagePath:@"resumecenter.png"imagePressPath:@"resumecenter_focus.png"type:Resumecenter]];
[self.tableDataSource addObject:[[MTSMenuModel alloc] init:@"薪酬咨询" imagePath:@"salaryquery.png" imagePressPath:@"salaryquery_focus.png" type:Salaryquery]];
[self.tableDataSource addObject:[[MTSMenuModel alloc] init:@"申请记录" imagePath:@"jobapply.png" imagePressPath:@"jobapply_focus.png" type:JobApply]];
[self.tableDataSource addObject:[[MTSMenuModel alloc] init:@"很多其它" imagePath:@"themore.png" imagePressPath:@"themore_focus.png" type:TheMore]];
}
@end
相关文章
- iOS小技能:封装定位SDK,统一数据模型和错误处理。
- Uni开发的app,使用Windows S10,放在ios上,全程跟着一个马平川。
- IOS APP开发:苹果app从开发到上架教程详解
- ios苹果app上架流程
- HBuilder X 连接苹果手机(IOS)详细教程。Windows: 连接iOS手机调试项目
- IOS – OPenGL ES 调节图像伽马线 GPUImageGammaFilter
- 【IOS实用玩机技巧】爱思助手 IPA 签名功能常见问题汇总(iOS上架)
- IOS – OpenGL ES 黑白马赛克效果 GPUImageMosaicFilter
- IOS – OpenGL ES 图像水晶球效果 GPUImageGlassSphereFilter
- 升级了Xcode 14.3 版本后Flutter运行IOS项目遇到File not found:arc/libarclite_iphoneos.a
- 聊聊苹果的Bug - iOS 10 nano_free Crash
- 图文介绍 Windows 系统下打包上传 IOS APP 流程
- AnyTrans for iOS for mac(ios数据传输管理工具)
- IOS开发之逆向分析详解手机开发
- ios入门之项目结构介绍详解手机开发
- Linux与iOS:同源不同道(linux与ios的关系)
- iOS与Linux:新老技术的融合(ios与linux的关系)
- 比较iOS和Linux:谁更具优势?(ios和linux)
- iOS访问MySQL数据库的实战技术(ios访问mysql)
- 把 Linux 安装到 iOS 设备上的步骤(ios安装linux)
- iOS 13.3 越狱有望:安全专家确认发现新tfp0漏洞
- 探秘iOS:它究竟是一种基于Linux的操作系统吗?(ios是linux系统吗)
- 移动端iOS技术实现Oracle数据库连接(ios联接oracle)
- iOS 轻松登录 Oracle 数据库(ios 登陆oracle)
- 谁说 iOS 就不适用Oracle(ios oracle软件)
- Reddit用户发现CSAM检测中使用的苹果NeuralHash算法隐藏在iOS 14.3中
- iOS开发之路--微博“更多”页面