Ios 项目从头开发 MVVM模式(三)

1.话说,本来想做个聚合查询功能,但是我的重点想研究xmpp聊天功能。所以使用mvvm模式做了完全模式51job主界面的页面。

2.首先给大家看我运行起来的界面。


3.界面很简单,做这个界面主要是为了比较mvvm模式和mvc模式之间的区别。

4.这个界面的结构是下边这张图片


与mvc相比,我多了一个viewmodel文件。

mvc之前是把业务逻辑和数据放在viewcontroller里边,逻辑复杂的话,别人维护起来很麻烦。

我就不贴viewcontroller的图片了,我把这个代码上传给大家,大家可以看看,和mvc相比,是不是很容易维护,代码层级会好一些。明天开始研究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:@"按钮测试"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




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值