#import "ViewController.h"
#import "XMGApp.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()
/** tableView的数据源*/
@property(nonatomic ,strong) NSArray *apps;
/** 图片缓存*/
@property(nonatomic ,strong) NSMutableDictionary *images;
/** */
@property(nonatomic ,strong) NSOperationQueue *queue;
@property(nonatomic ,strong) NSMutableDictionary *operations;
@end
@implementation ViewController
#pragma mark -------------------
#pragma mark lazy Loading
-(NSArray *)apps
{
if (_apps == nil) {
NSArray *appsArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]];
// NSLog(@"%@",appsArray);
NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:appsArray.count];
//字典转模型 字典数组--->模型数组
for (NSDictionary *dict in appsArray) {
[arrayM addObject:[XMGApp appWithDict:dict]];
}
_apps = arrayM;
}
return _apps;
}
-(NSMutableDictionary *)images
{
if (_images == nil) {
_images = [NSMutableDictionary dictionary];
}
return _images;
}
-(NSOperationQueue *)queue
{
if (_queue == nil) {
_queue = [[NSOperationQueue alloc]init];
_queue.maxConcurrentOperationCount = 5;
}
return _queue;
}
-(NSMutableDictionary *)operations
{
if (_operations == nil) {
_operations = [NSMutableDictionary dictionary];
}
return _operations;
}
#pragma mark -------------------
#pragma mark UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.apps.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.创建cell
static NSString *ID = @"app";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
//2.设置cell的数据
//2.1 拿到这个cell对应的数据
XMGApp *app = self.apps[indexPath.row];
//2.2 设置文字
cell.textLabel.text = app.name;
cell.detailTextLabel.text = app.download;
//2.3 设置图片
[cell.imageView sd_setImageWithURL:[NSURL URLWithString:app.icon] placeholderImage:[UIImage imageNamed:@"Snip20151124_183"]];
//拿到沙盒的路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
//文件名称
NSString *fileName = [app.icon lastPathComponent];
//拼接文件的全路径
NSString *fullPath = [caches stringByAppendingPathComponent:fileName];
NSLog(@"%@",fullPath);
//3.返回cell
return cell;
}
@end