Created
May 11, 2016 09:10
-
-
Save fhefh2015/488d0e96132851efdac88dcf03d17f64 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (NSArray *)datas { | |
if (_datas == nil) { | |
NSMutableArray *temp = [NSMutableArray array]; | |
NSString *path = [[NSBundle mainBundle] pathForResource:@"apps.plist" ofType:nil]; | |
NSArray *arr = [NSArray arrayWithContentsOfFile:path]; | |
for (NSDictionary *dict in arr) { | |
CellData *data = [CellData dataWithDict:dict]; | |
[temp addObject:data]; | |
} | |
_datas = temp; | |
} | |
return _datas; | |
} | |
- (NSMutableDictionary *)cacheDatas { | |
if (_cacheDatas == nil) { | |
_cacheDatas = [NSMutableDictionary dictionary]; | |
} | |
return _cacheDatas; | |
} | |
- (NSString *)savePath { | |
if (_savePath == nil) { | |
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject]; | |
_savePath = path; | |
} | |
return _savePath; | |
} | |
- (NSMutableDictionary *)operationDict { | |
if (_operationDict == nil) { | |
_operationDict = [NSMutableDictionary dictionary]; | |
} | |
return _operationDict; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { | |
return self.datas.count; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { | |
static NSString *cellID = @"cell"; | |
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID]; | |
if (cell == nil) { | |
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID]; | |
} | |
CellData *data = self.datas[indexPath.row]; | |
cell.textLabel.text = data.name; | |
cell.detailTextLabel.text = data.download; | |
NSString *fileName = [data.icon lastPathComponent]; | |
UIImage *image = self.cacheDatas[fileName]; | |
if (image) { | |
NSLog(@"m has images"); | |
cell.imageView.image = image; | |
} else { | |
image = [UIImage imageWithContentsOfFile:[self.savePath stringByAppendingPathComponent:fileName]]; | |
if (image) { | |
NSLog(@"m1 has images"); | |
cell.imageView.image = image; | |
self.cacheDatas[fileName] = image; | |
} else { | |
NSLog(@"m2 has images"); | |
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; | |
queue.maxConcurrentOperationCount = 2; | |
cell.imageView.image = [UIImage imageNamed:@"place"]; | |
NSBlockOperation *op = self.operationDict[fileName]; | |
if (op == nil) { | |
NSLog(@"m3 has images"); | |
op = [NSBlockOperation blockOperationWithBlock:^{ | |
NSLog(@"download image %@", [NSThread currentThread]); | |
NSURL *url = [NSURL URLWithString:data.icon]; | |
NSData *urlData = [NSData dataWithContentsOfURL:url]; | |
if (urlData == nil) { | |
return; | |
} | |
UIImage *image = [[UIImage alloc] initWithData:urlData]; | |
if (image) { | |
self.cacheDatas[fileName] = image; | |
} | |
[urlData writeToFile:[self.savePath stringByAppendingPathComponent:fileName] atomically:YES]; | |
NSOperationQueue *queueMain = [NSOperationQueue mainQueue]; | |
[queueMain addOperationWithBlock:^{ | |
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; | |
}]; | |
[self.operationDict removeObjectForKey:fileName]; | |
}]; | |
[queue addOperation:op]; | |
self.operationDict[fileName] = op; | |
} | |
} | |
} | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment