前言
今天和大家说一说model的一些问题。model相信大家都不默生,因为对于OC来说(其他语言都应该是一样的),MVC这个模式应该都是最基本的。只要用刀MVC那么你就要用到model。在我刚刚开始OC的时候,是一种写法,当自己感觉对OC有一点点自己的理解了之后,model的写法又是另一种。今天把我自己前后对于model的两种写法写出来,让大家参考一下。
Demo链接
正文
第一种model写法
一个model创建一个.m文件:代码如下(全文demo以一个简单的姓名表显示)
首先plist数据:
//姓名表的内层model,包括姓名,性别 .h声明
#import <Foundation/Foundation.h>
@interface SOldModel : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *sex;//性别
- (instancetype)initWithDic:(NSDictionary *)dic;
@end
//.m实现
#import "SOldModel.h"
@implementation SOldModel
/**
* 构造
*
* @param dic <#dic description#>
*
* @return <#return value description#>
*/
- (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
@end
上面是一个model文件,用于接收二层数据。
下面又是一个model文件,用于接收一层数据。
//姓名表的外层model .h声明
#import <Foundation/Foundation.h>
@interface FOldModel : NSObject
@property(nonatomic,copy)NSString *familyName;//姓氏
@property(nonatomic,strong)NSArray *messageArray;//信息
- (instancetype)initWithDic:(NSDictionary *)dic;
@end
//.m实现
#import "FOldModel.h"
#import "SOldModel.h"
@implementation FOldModel
/**
* 构造
*
* @param dic <#dic description#>
*
* @return <#return value description#>
*/
- (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dic];
//创建一个可变数组加载soldarray
NSMutableArray *soldArray = [NSMutableArray array];
for (NSDictionary *dic in self.messageArray) {
SOldModel *model = [[SOldModel alloc]initWithDic:dic];
[soldArray addObject:model];
}
self.messageArray = soldArray;
}
return self;
}
@end
然后在VC中显示
//.h声明
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)NSArray *foldArray;//数据
@end
//.m实现
#import "ViewController.h"
#import "FOldModel.h"
#import "SOldModel.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载表
[self loadTableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 数据加载
*/
-(NSArray *)foldArray
{
if (_foldArray == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
NSArray *oldArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *newArray = [NSMutableArray array];
for (NSDictionary *dic in oldArray) {
FOldModel *model = [[FOldModel alloc]initWithDic:dic];
[newArray addObject:model];
}
_foldArray = newArray;
}
return _foldArray;
}
/**
* 加载表的方法
*/
- (void)loadTableView
{
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
self.tableView = tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.foldArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
FOldModel *fModel = self.foldArray[section];
return fModel.messageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * const ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
FOldModel *fModel = self.foldArray[indexPath.section];
SOldModel *sModel = fModel.messageArray[indexPath.row];
cell.textLabel.text = sModel.name;
cell.detailTextLabel.text = sModel.sex;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
FOldModel *fModel = self.foldArray[section];
return fModel.familyName;
}
@end
运行结果:
第二种model写法
所有的model在一个文件创建
//.h声明
#import <Foundation/Foundation.h>
//第一个model
@interface NewModel : NSObject
@property(nonatomic,copy)NSString *familyName;//姓氏
@property(nonatomic,strong)NSArray *messageArray;//信息
- (instancetype)initWithDic:(NSDictionary *)dic;
@end
//第二个model
@interface NewModel2 : NSObject
@property(nonatomic,copy)NSString *name;//姓名
@property(nonatomic,copy)NSString *sex;//性别
- (instancetype)initWithDic:(NSDictionary *)dic;
@end
//.m实现
#import "NewModel.h"
// 第一个model
@implementation NewModel
/**
* 构造
*
* @param dic <#dic description#>
*
* @return <#return value description#>
*/
- (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dic];
//创建一个可变数组加载soldarray
NSMutableArray *newArray = [NSMutableArray array];
for (NSDictionary *dic in self.messageArray) {
NewModel2 *model = [[NewModel2 alloc]initWithDic:dic];
[newArray addObject:model];
}
self.messageArray = newArray;
}
return self;
}
@end
//第二个model
@implementation NewModel2
/**
* 构造
*
* @param dic <#dic description#>
*
* @return <#return value description#>
*/
- (instancetype)initWithDic:(NSDictionary *)dic
{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dic];
}
return self;
}
@end
大家可以看到,两个model都在同一个文件进行了声明实现。相对于第一个而言,第二种方法要简洁的很多。如果有多个model,在xcode的目录显示第二种方法要好得多。
显示数据:
//.h声明
#import <UIKit/UIKit.h>
@interface NewViewController : UIViewController
@property(nonatomic,weak)UITableView *tableView;
@property(nonatomic,strong)NSArray *foldArray;//数据
@end
//.m实现
#import "NewViewController.h"
#import "NewModel.h"
@interface NewViewController ()<UITableViewDataSource,UITableViewDelegate>
@end
@implementation NewViewController
- (void)viewDidLoad {
[super viewDidLoad];
//加载表
[self loadTableView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/**
* 数据加载
*/
-(NSArray *)foldArray
{
if (_foldArray == nil) {
NSString *path = [[NSBundle mainBundle]pathForResource:@"data.plist" ofType:nil];
NSArray *oldArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *newArray = [NSMutableArray array];
for (NSDictionary *dic in oldArray) {
NewModel *model = [[NewModel alloc]initWithDic:dic];
[newArray addObject:model];
}
_foldArray = newArray;
}
return _foldArray;
}
/**
* 加载表的方法
*/
- (void)loadTableView
{
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
self.tableView = tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.foldArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NewModel *fModel = self.foldArray[section];
return fModel.messageArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * const ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
NewModel *fModel = self.foldArray[indexPath.section];
NewModel2 *sModel = fModel.messageArray[indexPath.row];
cell.textLabel.text = sModel.name;
cell.detailTextLabel.text = sModel.sex;
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NewModel *fModel = self.foldArray[section];
return fModel.familyName;
}
@end
运行结果:
结语
大家可以看到,两种方式的数据model都可以很好的显示。我自己选择的是第二种方式。