字典转模型->把每一个模型存到数组->用indexPath访问数组的每一个对象并显示到cell上

字典转模型 就是利用for in遍历将字典中的每一个key value和模型中的属性匹配,如何匹配?利用模型的对象调用setvaluesforkeyswithdictionary方法。注意返回值肯定是模型的类型啊(常识),如果匹配的到,就表示符合将字典转成模型,然后将数据存进数组中保存,数组中的内容肯定是模型的类型。然后通过在cellforrowatindex 从模型中取出被indexpath.row标识的每一行数据显示到每个cell上。模型中的有多少个数据,那么cellforrowatindex就掉用多少次。细节问题:取出被indexpath.row标识的每一行,这些行的中的数据肯定是模型类型的数据,上面有提及。然后自定义的cell对象掉用setter方法并将模型类型的对象作为参数实现设置数据到cell上。因为每调用一次cellforrowatindex就会调用自定义cell的setter方法,把某一行的数据显示到cell上,所以数组中存储的所有数据都会按标识的顺序一次显示到cell上
精华:从转模型到使用数组中的模型数据的总过程为:将请求到的数据进行字典转模型然后存进去数组,这样以后就可以通过数组的下标拿到模型,然后可以通过模型的对象访问属性(实际上访问的数组中指定数据),从而拿到数组中指定的数据了。


字典转模型的三种做法

做法1:手动声明和实现字典转模型的方法,自行进行key value的匹配

#import "ViewController.h"
#import "ZB_Model.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *responseObject = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses" ofType:@"plist"]];
    
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    // 遍历responseObject数组中的内容,因为responseObject数组中装的全是字典,所以利用for in遍历出每一个dic
    for (NSDictionary *dic in responseObject) {
        // 打印字典中的内容
        NSLog(@"%@",dic);
        ZB_Model *statusesModel = [ZB_Model returnVideoModelWithDictionary:dic];
        [tempArray addObject:statusesModel];
    }
    self.DataArray = tempArray;
    NSLog(@"%@",self.DataArray);
}

@end
#import 

@interface ZB_Model : NSObject

/** 新增加的smart_img 图片*/
@property(nonatomic,copy)NSString *smart_img;
/** 新增加的smart_img1 图片*/
@property(nonatomic,copy)NSString *smart_img1;
/** 新增加的smart_img2 图片*/
@property(nonatomic,copy)NSString *smart_img2;
/** 新增加的title  标题 */
@property(nonatomic,copy)NSString *title;
/** 新增加的source_name 来源名称 */
@property(nonatomic,copy)NSString *source_name;
/** 新增加的source_ico 来源图标 */
@property(nonatomic,copy)NSString *source_ico;

/** 新增加的type_id  1资讯2视频 */
@property (nonatomic, assign) int type_id;
/** 新增加的sys_tag 展示标签 1精,2热,3广告 */
@property(nonatomic,copy)NSString *sys_tag;
/** 新增加的url 跳转地址 */
@property(nonatomic,copy)NSString *url;

/*** 浏览人数 */
@property(nonatomic, copy) NSString *viewnum;

/** 图片*/
@property(nonatomic,copy)NSString *ImgDic3;

+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict;

@end


#import "ZB_Model.h"

@implementation ZB_Model

+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict{
    ZB_Model *model = [[ZB_Model alloc] init];
    // 取出dic字典中的text对应的value,放到模型的contentStr属性中存储
    model.title = dict[@"title"];
    model.viewnum = dict[@"viewnum"];
    model.source_name = dict[@"source_name"];
    model.ImgDic3 = dict[@"imageDic"][@"ImgDic3"];

    return model;
}

@end

做法2手动声明和实现字典转模型的方法并调用setValuesForKeysWithDictionary:

#import "ViewController.h"
#import "ZB_Model.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *responseObject = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"statuses" ofType:@"plist"]];
    NSMutableArray *tempArray = [[NSMutableArray alloc] init];
    // 遍历responseObject数组中的内容,因为responseObject数组中装的全是字典,所以利用for in遍历出每一个dic
    for (NSDictionary *dic in responseObject) {
        // 打印字典中的内容
        NSLog(@"%@",dic);
        // 将字典转成模型
        ZB_Model *statusesModel = [ZB_Model returnVideoModelWithDictionary:dic];
        // 模型存到临时数组中
        [tempArray addObject:statusesModel];
        
    }
    // 临时数组赋值给全局的数组
    self.DataArray = tempArray;
        // 遍历全局数组的内容。经打印可以的得知,内容是ZB_Model类型的数据
        for (NSString *i in self.DataArray) {
            NSLog(@"%@",i);
        }
}

@end

#import 

@interface ZB_Model : NSObject

/** 新增加的smart_img 图片*/
@property(nonatomic,copy)NSString *smart_img;
/** 新增加的smart_img1 图片*/
@property(nonatomic,copy)NSString *smart_img1;
/** 新增加的smart_img2 图片*/
@property(nonatomic,copy)NSString *smart_img2;
/** 新增加的title  标题 */
@property(nonatomic,copy)NSString *title;
/** 新增加的source_name 来源名称 */
@property(nonatomic,copy)NSString *source_name;
/** 新增加的source_ico 来源图标 */
@property(nonatomic,copy)NSString *source_ico;

/** 新增加的type_id  1资讯2视频 */
@property (nonatomic, assign) int type_id;
/** 新增加的sys_tag 展示标签 1精,2热,3广告 */
@property(nonatomic,copy)NSString *sys_tag;
/** 新增加的url 跳转地址 */
@property(nonatomic,copy)NSString *url;

/*** 浏览人数 */
@property(nonatomic, copy) NSString *viewnum;

/** 图片*/
@property(nonatomic,copy)NSString *ImgDic3;

+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict;

@end


#import "ZB_Model.h"

@implementation ZB_Model

+(ZB_Model *)returnVideoModelWithDictionary:(NSDictionary *)dict{
    
    ZB_Model *model = [[ZB_Model alloc] init];
    
    [model setValuesForKeysWithDictionary:dict];
    return model;
}

/*
 不写下面这句代码,会报如下的错误。写了下面的代码,即使plist中有A字段,而模型中没有A字段也不会报错。
 '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key imageDic.'
 */
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
    
}

@end

做法3:利用框架进行字典转模型,不需要手动声明和实现字典转模型的方法

#import "ViewController.h"
#import "ZB_Model.h"
#import "MJExtension/MJExtension.h"
@interface ViewController ()
@property(nonatomic,strong)NSMutableArray *DataArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 将字典转成模型
    self.DataArray = [ZB_Model mj_objectArrayWithFilename:@"statuses.plist"];
    NSLog(@"%ld",self.DataArray.count);
    NSLog(@"%@",self.DataArray);
   }

@end

#import 

@interface ZB_Model : NSObject

/** 新增加的smart_img 图片*/
@property(nonatomic,copy)NSString *smart_img;
/** 新增加的smart_img1 图片*/
@property(nonatomic,copy)NSString *smart_img1;
/** 新增加的smart_img2 图片*/
@property(nonatomic,copy)NSString *smart_img2;
/** 新增加的title  标题 */
@property(nonatomic,copy)NSString *title;
/** 新增加的source_name 来源名称 */
@property(nonatomic,copy)NSString *source_name;
/** 新增加的source_ico 来源图标 */
@property(nonatomic,copy)NSString *source_ico;

/** 新增加的type_id  1资讯2视频 */
@property (nonatomic, assign) int type_id;
/** 新增加的sys_tag 展示标签 1精,2热,3广告 */
@property(nonatomic,copy)NSString *sys_tag;
/** 新增加的url 跳转地址 */
@property(nonatomic,copy)NSString *url;

/*** 浏览人数 */
@property(nonatomic, copy) NSString *viewnum;

/** 图片*/
@property(nonatomic,copy)NSString *ImgDic3;

@end


[LS](https://pan.baidu.com/s/1pKSiEZD 密码 tmsq)

你可能感兴趣的:(字典转模型->把每一个模型存到数组->用indexPath访问数组的每一个对象并显示到cell上)