MVC(模型-视图-控制器)的实现

来源:互联网 发布:dota2新人选手知乎 编辑:程序博客网 时间:2024/04/30 09:17

MVC(模型-视图-控制器)是一种设计思想,贯穿于整个ios开发当中,当积累了一定的开发经验时,你就能深刻的领会MVC(模型-视图-控制器)当中的好处和真正含义。

MVC(模型-视图-控制器)主要有三个角色:

》M:模型数据(Model)

》V:视图或界面(View)

》C:控制器(Control)

MVC(模型-视图-控制器)的明显特征:

》View上显示什么内容完全取决于Model;

》一旦Model数据发生改变,View的显示状态也会跟着发生变化;

》Control负责初始化Model,并且将Model传递给View去解析和展示;

假设利用这一思想来重构代码,将plist文件中的字典转化成模型,代码如下:

code:

首先创建一个class:ZHLCosmeticsGroup

ZHLCosmeticsGroup.h:

#import <Foundation/Foundation.h>@interface ZHLCosmeticsGroup : NSObject/** *  头部标题 */@property (nonatomic, copy) NSString *title;/** *  尾部标题(描述) */@property (nonatomic, copy) NSString *description;/** *  这组的所有化妆品牌(字符串) */@property (nonatomic, strong) NSArray *Cosmetics;@end


ViewCotroller.m文件:

#import <Foundation/Foundation.h>@interface ZHLCosmeticsGroup : NSObject#import "ViewController.h"#import "ZHLCosmeticsGroup.h"@interface ViewController () <UITableViewDataSource>@property (nonatomic, strong) NSArray *CosmeticsGroups;@end@implementation ZHLViewController- (void)viewDidLoad{    [super viewDidLoad];        // 设置数据源    self.tableView.dataSource = self;}// 隐藏状态栏- (BOOL)prefersStatusBarHidden{    return YES;}- (NSArray *)CosmeticsGroups{    if (_CosmeticsGroups == nil) {        // 初始化        // 法国品牌        ZHLCosmeticsGroup *cg1 = [[ZHLCosmeticsGroup alloc] init];        cg1.title = @"法国品牌";        cg1.description = @"都是女生想要的";        cg1.Cosmetics = @[@"迪奥", @"兰蔻", @"薇姿", @"香奈儿"];                // 日韩品牌        ZHLCosmeticsGroup *cg2 = [[ZHLCosmeticsGroup alloc] init];        cg2.title = @"日韩品牌";        cg2.description = @"韩妆棒棒哒";        cg2.Cosmetics = @[@"梦妆",@"whoo后" @"SK-II", @"植村秀", @"资生堂",@"Sensai"];                // 欧系品牌        ZHLCosmeticsGroup *cg3 = [[ZHLCosmeticsGroup alloc] init];        cg3.title = @"欧美品牌";        cg3.description = @"都是大牌";        cg3.Cosmetics = @[@"雅诗兰黛", @"倩碧"];                _CosmeticsGroups = @[cg1, cg2, cg3];    }    return _CosmeticsGroups;}#pragma mark - 数据源方法/** *  一共有多少组数据 */- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{    return self.CosmeticsGroups.count;}/** *  第section组有多少行 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    // 取得第section组对应的模型    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[section];        return cg.Cosmetics.count;}/** *  每一行显示怎样的内容(cell) */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];        // 取出第indexPath.section组对应的模型    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[indexPath.section];        // 取车第indexPath.row这行对应的品牌名称    NSString *cos = cg.Cosmetics[indexPath.row];        // 设置cell显示的文字    cell.textLabel.text = cos;        return cell;}/** *  第section组显示怎样的头部标题 */- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[section];        return cg.title;}/** *  第section组显示怎样的尾部标题 */- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{    ZHLCosmeticsGroup *cg = self.CosmeticsGroups[section];        return cg.description;}@end



4 2
原创粉丝点击