Controller减压

思路

MVC分工明确,各司其职。
Model里面进行异步请求数据,不要在controller里面进行操作。
Model里进行网络请求和数据解析,那么问题了,项目越做越大,会有好多个Model,如果分别在不同的Model里进行网络请求和数据解析,还是难以查找维护,所以,创建一个NetTools专门进行网络请求和数据解析,把Model头文件导过去就可以了。

我见过太多的代码都是在controller里面进行异步请求数据,更有甚者,在每个controller里面进行数据解析,简单的几个功能,controller里面代码上千行,毫无封装可言!
写这篇文章是想彻底的解决这样低质量代码。
本文由浅到深,先使用plist数据,以便与异步请求数据形成对比.
随后另写一篇文章写一下异步请求数据如何封装,以及使用plist和异步请求数据的原理,区别,涉及到程序执行的先后顺序。
懒加载的深刻理解,
异步的深刻理解!
只有理解了原理,写的的代码才够扎实,遇到问题才知道怎么解决。
而不是仅仅模仿套路来实现功能,事实上,好多"套路"是有问题的,结构不清晰的。
只有研究出自己的套路才是最好的。
这个Demo没有JSON,我却使用了JSONModel,之前一直用MJExtension,用JSONModel比较少,想尝试下JSONModel。
效果图:

效果图.png

数据结构:

数据结构.png

Model:(使用了JSONModel三方框架)

h文件
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "JSONModel.h"


@interface CarModel : JSONModel
@property (nonatomic,copy)NSString * name;
@end

m文件不用做任何操作,这就是使用JSONModel的优势

另一个Model

h文件
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "JSONModel.h"

@protocol CarModel;

@interface GroupModel : JSONModel

@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * footer;
@property (nonatomic)NSArray<CarModel> * cars;


+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error;

@end

m文件
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "GroupModel.h"

@implementation GroupModel

+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error{

    NSString * path = [[NSBundle mainBundle] pathForResource:@"wjTest.plist" ofType:nil];
    NSArray * arrayDict = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray * arrayModels = [NSMutableArray array];
    for (NSDictionary * dic in arrayDict) {
        GroupModel * model = [[GroupModel alloc]initWithDictionary:dic error:nil];
        
        //NSLog(@"model--%@",model.cars);
        [arrayModels addObject:model];
    }
   
    if (success) {
        success(arrayModels.copy);
    }

}


@end


Controller

//
//  ViewController.m
//  TableView
//
//  Created by Mac on 2017/8/6.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "ViewController.h"
#import "GroupModel.h"
#import "CarModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSArray * groups;
@property (nonatomic,strong)UITableView * tableView;
@end

@implementation ViewController


- (void)setArray:(NSArray *)array{
    _groups = array;
    // 重新加载
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    [self createTableView];
    
}

- (void)initData{
    
    [GroupModel wangjie:^(NSArray *array) {
        self.groups = array;
        NSLog(@"成功");
    } error:^{
        NSLog(@"出错了");
    }];

}


- (void)createTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
}



#pragma mark - 段数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.groups.count;
}


#pragma mark - 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    GroupModel * model = self.groups[section];
    return model.cars.count;
}


#pragma mark - 行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GroupModel * groupModel = self.groups[indexPath.section];
    CarModel * carModel = groupModel.cars[indexPath.row];
    static NSString * ID = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.textLabel.text = carModel.name;
    return cell;

}


#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 44;
}


#pragma mark - 段头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    GroupModel * model = self.groups[section];
    return model.title;
}


#pragma mark - 段脚高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}


#pragma mark - 段脚
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    GroupModel * model = self.groups[section];
    return model.footer;
}


#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden{
    return YES;
}


@end

使用了UITableViewCell,所以没有自定义cell,也不用封装View
以后写关于View极致的封装

其实,Model还可以这样写,少写一个文件
把两个Model合并,可以少创建一个文件

h文件
// 
//  CarModel.h
//  TableView
//
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "JSONModel.h"

@protocol CarModel;

@interface CarModel : JSONModel
@property (nonatomic,copy)NSString * name;

@end

@interface GroupModel : JSONModel

@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * footer;
@property (nonatomic)NSArray<CarModel> * cars;


+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error;


@end


m文件

//
//  CarModel.m
//  TableView
//
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "CarModel.h"

@implementation CarModel

@end


@implementation GroupModel

+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error{
    
    NSString * path = [[NSBundle mainBundle] pathForResource:@"wjTest.plist" ofType:nil];
    NSArray * arrayDict = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray * arrayModels = [NSMutableArray array];
    for (NSDictionary * dic in arrayDict) {
        GroupModel * model = [[GroupModel alloc]initWithDictionary:dic error:nil];
        
        //NSLog(@"model--%@",model.cars);
        [arrayModels addObject:model];
    }
    
    if (success) {
        success(arrayModels.copy);
    }
    
}


@end

也可以这样


//
//  GroupModel.h
//  TableView
//
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "JSONModel.h"

@protocol CarModel;

@interface GroupModel : JSONModel

@property (nonatomic,copy)NSString * title;
@property (nonatomic,copy)NSString * footer;
@property (nonatomic)NSArray<CarModel> * cars;


+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error;

@end


@interface CarModel : JSONModel

@property (nonatomic,copy)NSString * name;


@end



//
//  GroupModel.m
//  TableView
//
//  Created by Mac on 2017/8/29.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "GroupModel.h"

@implementation GroupModel

+ (void)wangjie:(void(^)(NSArray * array))success error:(void(^)())error{

    NSString * path = [[NSBundle mainBundle] pathForResource:@"wjTest.plist" ofType:nil];
    NSArray * arrayDict = [NSArray arrayWithContentsOfFile:path];
    
    NSMutableArray * arrayModels = [NSMutableArray array];
    for (NSDictionary * dic in arrayDict) {
        GroupModel * model = [[GroupModel alloc]initWithDictionary:dic error:nil];
        
        //NSLog(@"model--%@",model.cars);
        [arrayModels addObject:model];
    }
   
    if (success) {
        success(arrayModels.copy);
    }

}


@end



@implementation CarModel

@end

2017年10月11日18:41:14 更新

行内容中的代码优化:
参考:UITableView和UITableViewCell容易混淆的几个方法

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 17,122评论 4赞 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 181,003评论 25赞 708
  • 看过一则故事, 有一只觅食的狐狸, 顺着香味找到一片茂盛的葡萄架, 架上葡萄溜圆晶亮, 馋得狐狸垂涎欲滴。 于是狐...
    旖旎i阅读 323评论 0赞 4
  • 对于现在的00后来说,可能会叫不全所有人的名字。更可恶的是,张冠李戴到某个小鲜肉的头上。记得我小时候,看到梁朝伟很...
    热心村民陈某阅读 522评论 0赞 0
  • 屠格涅夫(1818一1883)俄罗斯著名的现实主义作家,与列夫•托尔斯泰齐名。代表作有《猎人笔记》,《父与子》,《...
    泰山寒梅阅读 7,636评论 14赞 22

友情链接更多精彩内容