Giter Club home page Giter Club logo

mvvmframework's Introduction

#MVVMFramework ###Swift版本地址:https://github.com/lovemo/MVVMFramework-Swift ####本项目交流群:474292335 ####欢迎有兴趣的有好的想法的参与到项目中来

再看了几篇博客后,总结整理下一个快速开发MVVM框架(抛砖引玉),分离控制器代码,降低代码耦合

终于再也不用为ViewController中一坨坨tableView和collectionView的烦人代码忧虑了

代码加入了cell自适应高度,自动缓存网络请求至sqlite数据库,运行时自动布局UILabel,配合MJExtension,MJRefresh,AFNetworking等常用开发框架使用更佳,主要用于分离控制器中的代码,降低代码耦合程度,可以根据自己使用习惯调整代码。欢迎来喷,提issues。

##思维流程图示 image image ##现在的工程代码结构 image

模块构建


结构分析


代码示例

一句代码集成展示tableView,cell自适应高度,下拉刷新

      self.table.tableHander = [[MVVMTableDataDelegate alloc]initWithViewModel:[[BQViewModel alloc]init]
                                        cellIdentifiersArray:@[MyCellIdentifier]
                                        didSelectBlock:^(NSIndexPath *indexPath, id item) {
                                                               
                                        SecondVC *vc = (SecondVC *)[UIViewController viewControllerWithStoryBoardName:@"Main" identifier:@"SecondVCID"];
                                        [weakSelf.navigationController pushViewController:vc animated:YES];
                                        NSLog(@"click row : %@",@(indexPath.row)) ;
                                        }];

一句代码集成展示collectionView

     self.collectionView.collectionHander = [[MVVMCollectionDataDelegate alloc]initWithViewModel:[[BQViewModel2 alloc]init]
                                                            cellIdentifier:MyCellIdentifier
                                                            collectionViewLayout:nil cellItemSizeBlock:^CGSize {
                                                                return CGSizeMake(110, 120);
                                                            } cellItemMarginBlock:^UIEdgeInsets {
                                                                return UIEdgeInsetsMake(0, 20, 0, 20);
                                                            } didSelectBlock:^(NSIndexPath *indexPath, id item) {
                                                                NSString *strMsg = [NSString stringWithFormat:@"click row : %zd",indexPath.row];
                                                                [[[UIAlertView alloc] initWithTitle:@"提示"
                                                                                    message:strMsg
                                                                                    delegate:self
                                                                                    cancelButtonTitle:@"好的"
                                                                                    otherButtonTitles:nil, nil] show];
                                                            }];

一句代码实现网络请求,自动缓存网络请求数据

    NSString *url = @"http://news-at.zhihu.com/api/4/news/latest";
    [MVVMHttp get:url params:nil cachePolicy:MVVMHttpReturnCacheDataThenLoad success:^(id responseObj) {
        
        NSArray *array = responseObj[@"stories"];
        self.dataArrayList = [ThirdModel mj_objectArrayWithKeyValuesArray:array];
        if (successHandler) {
            successHandler();
        }
        
    } failure:^(NSError *error) {
        
    }];

几行代码实现数据存储

    MVVMStore *store = [[MVVMStore alloc]init];
    [store db_initWithDBName:@"demo.sqlite" tableName:@"arrarList"];
    [store db_putObject:array withId:@"arrayID" intoTable:@"arrarList"];

demo效果

  • 只需实现加载请求以及配置自定义cell和上述代码,就能轻松实现以下效果,最重要的是代码解耦。

image

使用方法

  • 拖拽MVVM文件夹,然后在模块代码中新建ViewModel子类,继承MVVMBaseViewModel类型,实现加载数据等方法。
  • 根据需要继承MVVMTableDataDelegate或MVVMCollectionDataDelegate扩展方法
  • 在Controller中,初始化tableView或者collectionView,根据需要实现block代码,将自动根据传入的内容去展现数据。
  • 利用xib自定义cell,在- (void)configure:customObj:indexPath:方法中根据模型Model内容配置cell展示的数据。

期待

  • 如果在使用过程中遇到BUG,希望你能Issues我,谢谢(或者尝试下载最新的代码看看BUG修复没有)
  • 如果在使用过程中发现功能不够用,希望你能Issues我,我非常想为这个框架增加更多好用的功能,谢谢

推荐-几篇不错的MVVM学习文章

mvvmframework's People

Contributors

lovemo avatar randomprocess avatar 1074653827 avatar

Watchers

赵磊 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.