字典轉模型->把每一個模型存到數組->用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 <Foundation/Foundation.h>

@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 <Foundation/Foundation.h>

@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字段也不會報錯。
 '[<ZB_Model 0x6080000a4320> 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 <Foundation/Foundation.h>

@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)

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,565評論 6 539
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,115評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,577評論 0 382
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,514評論 1 316
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,234評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,621評論 1 326
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,641評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,822評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,380評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,128評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,319評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,879評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,548評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,970評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,229評論 1 291
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,048評論 3 397
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,285評論 2 376

推薦閱讀更多精彩內容

  • 第5章 引用類型(返回首頁) 本章內容 使用對象 創建并操作數組 理解基本的JavaScript類型 使用基本類型...
    大學一百閱讀 3,263評論 0 4
  • *面試心聲:其實這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個offer,總結起來就是把...
    Dove_iOS閱讀 27,197評論 30 471
  • 返京。 省了早起,也省了沒帶身份證的麻煩,前一天試著約車,約到的是邯鄲過來的順風車,司機繞了很遠的路,半天才接到我...
    還好你來了閱讀 303評論 0 0
  • 一天下午,在閨蜜家喝下午茶。 男孩們擠到一個房間里,玩電腦游戲。 媽媽們坐在餐桌旁,吃橘子,聊怎么養多肉。 閨蜜的...
    唐硯閱讀 2,337評論 24 102
  • 首先,對你說聲抱歉,因為下載簡書申請賬號后,一直擱置在手機里面了,對這個app不怎么熟悉,昨天打開無意中日刷到那篇...
    cindyyyy閱讀 220評論 0 1