在cocoachina 上看到一篇文章 http://www.cocoachina.com/newbie/basic/2012/0511/4237.html UITableView 异步加载图片缓存
正好我也在学习UITableView 所以模仿了一个 拿来练手
- (void)viewDidLoad
{
[super viewDidLoad];
self.listTableViewArray = [NSMutableArray arrayWithCapacity:1];
NSString *jsonStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/json"] encoding:NSUTF8StringEncoding error:nil];
NSDictionary *dic = [jsonStr JSONValue];//通过json 从网络上获得数据
self.listTableViewArray = [[dic objectForKey:@"feed"]objectForKey:@"entry"];
table = [[[UITableView alloc]initWithFrame:CGRectMake(100, 100, 320, 480) ]autorelease];
[self.view addSubview:table];
table.backgroundColor = [UIColor yellowColor];
table.delegate = self; // 设置代理
table.dataSource = self;// 设置数据源
// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listTableViewArray count];//返回cell的函数
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"ddddd";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]autorelease];
}
NSDictionary *item = [self.listTableViewArray objectAtIndex:indexPath.row];
cell.textLabel.text =[[item objectForKey:@"im:name"]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:indexPath.row] valueForKey:@"im:name"] valueForKey:@"label"] stringByAppendingString:@".temp"];
//先到Library/Caches/ 目录下找图片 如果没有 就使用默认图片
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imageDataPath]];
if (image) {
cell.imageView.image = image;
}
else {
cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];
//placeholder为在未加载完成加载图片时显示的默认图片
}
return cell;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
// 因为UITableView 继承的是UIScrollView 所以继承方法scrollViewDidEndDecelerating 这个方法是 拖拽 scroll之后 完成减速时执行
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil]; //另开线程 执行加载cell里的图片
NSLog(@"scrollViewDidEndDecelerating");
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{//行将显示的时候调用
if (!tableView.isDragging && !tableView.isDecelerating)
{
[self performSelectorInBackground:@selector(loadCellImage) withObject:nil];
NSLog(@" table view willDisplayCell");
}
}
-(void)loadCellImage
{
NSArray *indexPathsForLoad = [table indexPathsForVisibleRows];// 获得 table 当前显示的cells
for (NSIndexPath *item in indexPathsForLoad) {
NSInteger rowNumberForCell = item.row;
NSString *imageStr = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]objectForKey:@"im:image"]objectAtIndex:0]objectForKey:@"label"];
NSString *imageName = [[[[self.listTableViewArray objectAtIndex:rowNumberForCell]valueForKey:@"im:name"]valueForKey:@"label"]stringByAppendingString:@".temp"];
NSString *imageDataPath = [NSHomeDirectory() stringByAppendingPathComponent:[@"Library/Caches/" stringByAppendingString:imageName]];
if (![[NSFileManager defaultManager]fileExistsAtPath:imageDataPath]) {//先判断Library/Caches/ 这个目录下有没有这个文件如果没有 就写入目录
NSURL *imageurl = [NSURL URLWithString:[imageStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *imageData = [NSData dataWithContentsOfURL:imageurl];
[imageData writeToFile:imageDataPath atomically:YES];
UIImage *image = [UIImage imageWithData:imageData];
UITableViewCell *cell = [table cellForRowAtIndexPath:item];
cell.imageView.image = image;// 更新cell的 image
}
}
}