#import <Foundation/Foundation.h> @interface DDZApp : NSObject //图标 @property (nonatomic,strong) NSString *icon; //名字 @property (nonatomic,strong) NSString *name; //下载量 @property (nonatomic,strong) NSString *download; + (instancetype)appWithDict:(NSDictionary *)dict; @end
#import "DDZApp.h"
@implementation DDZApp
+ (instancetype)appWithDict:(NSDictionary *)dict {
DDZApp *app = [[self alloc] init];
[app setValuesForKeysWithDictionary:dict];
return app;
}
@end
@interface ViewController () //所有数据 @property (nonatomic,strong)NSArray *apps; //内存缓存图片 @property (nonatomic,strong)NSMutableDictionary *imgCache; @end
@implementation ViewController
//读取数据
- (NSArray *)apps {
if (!_apps) {
//从plist文件中读取数据
NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
[appArray addObject:[DDZApp appWithDict:dict]];
}
_apps = appArray;
}
return _apps;
}
//缓存图片
- (NSMutableDictionary *)imgCache {
if (!_imgCache) {
//初始化
_imgCache = [NSMutableDictionary dictionary];
}
return _imgCache;
}
#pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.apps.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
DDZApp *app = self.apps[indexPath.row];
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
//先从内存中取出图片
UIImage *image = self.imgCache[app.icon];
if (image) {
cell.imageView.image = image;
}else {
//内存中没有图片
//将图片文件数据写入到沙盒中
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
//获得文件名
NSString *filename = [app.icon lastPathComponent];
//计算出文件的全路径
NSString *file = [cachesPath stringByAppendingPathComponent:filename];
//加载沙盒的文件数据
NSData *data = [NSData dataWithContentsOfFile:file];
//判断沙盒中是否有图片
if (data) {
//直接加载沙盒中图片
cell.imageView.image = [UIImage imageWithData:data];
//存到字典(内存)中
self.imgCache[app.icon] = cell.imageView.image;
}else {
//下载图片
data = [NSData dataWithContentsOfURL:[NSURL URLWithString:app.icon]];
cell.imageView.image = [UIImage imageWithData:data];
//存到内存中
self.imgCache[app.icon] = cell.imageView.image;
//将图片数据写入到沙盒中
[data writeToFile:file atomically:YES];
}
}
return cell;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有