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
- MVC(模型-视图-控制器)的实现
- MVC(模型---视图---控制器)
- 模型,视图,控制器(MVC)
- ASP.NET中实现模型-视图-控制器(MVC)模式
- MVC(模型-视图-控制器)
- 模型-视图-控制器 (MVC)
- 模型-视图-控制器 (MVC)
- 模型-视图-控制器 (MVC)
- 模型-视图-控制器(MVC)
- mvc只控制器,模型,视图的搭建
- 模型-视图-控制器(MVC)
- 模型-视图-控制器(MVC)封装逻辑
- MVC 模型-视图-控制器体系结构
- yii 模型-视图-控制器 (MVC)
- 2.模型-视图-控制器 (MVC)
- MVC之父对“模型-视图-控制器”的最初定义
- MVC之父对“模型-视图-控制器”的最初定义
- 简单介绍下mvc的路由,加载控制器,模型,视图
- Learning Depth from Single Monocular Images Using Deep Convolutional Neural Fields
- hello word
- runtime为类目扩展属性
- PHP之面向对象的程序设计(一)
- maven install 报错解决
- MVC(模型-视图-控制器)的实现
- iframe刷新父页面
- H2Database之h2-xxx.jar分析
- C++编程笔记:dll的生成与使用
- Hibernate中日期类型字段的映射策略
- 简单聊聊Handler机制
- Linux tcpdump命令详解
- What is M3U8
- SQL语句基础教程。