一、Mode建立(demo下载)
- 模型.h文件(继承自NSObject)
#import <Foundation/Foundation.h>
@interface XMGAPP : NSObject
/** APP的名称 */
@property (nonatomic, strong) NSString *name;
/** APP的图片的url地址 */
@property (nonatomic, strong) NSString *icon;
/** APP的下载量 */
@property (nonatomic, strong) NSString *download;
+(instancetype)appWithDict:(NSDictionary *)dict;
@end
- 模型.m文件
- dic字典——>M模型
setValuesForKeysWithDictionary
#import "XMGAPP.h"
@implementation XMGAPP
+(instancetype)appWithDict:(NSDictionary *)dict
{
XMGAPP *appM = [[XMGAPP alloc]init];
//KVC
[appM setValuesForKeysWithDictionary:dict];
return appM;
}
@end
二、在VC控制器调用Model
1 、在需要的VC控制器页面懒加载数组:(字典
数组转为模型
数组)
-(NSArray *)apps
{
if (_apps == nil) {
//字典数组
NSArray *arrayM = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"apps.plist" ofType:nil]];
//字典数组---->模型数组
NSMutableArray *arrM = [NSMutableArray array];
for (NSDictionary *dict in arrayM) {
[arrM addObject:[XMGAPP appWithDict:dict]];
}
_apps = arrM;
}
return _apps;
}
- 分析:
将dic转存Mode ,Mode放入Arr:
[arrM addObject:[XMGAPP appWithDict:dict]];
2 .在VC控制器的cell中调用
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"app";
//1.创建cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.设置cell的数据
//2.1 拿到该行cell对应的数据
XMGAPP *appM = self.apps[indexPath.row];
//2.2 设置标题
cell.textLabel.text = appM.name;
//2.3 设置子标题
cell.detailTextLabel.text = appM.download;
//2.4 设置图标
NSURL *url = [NSURL URLWithString:appM.icon];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
cell.imageView.image = image;
NSLog(@"%zd-----",indexPath.row);
//3.返回cell
return cell;
}