iphone table 实现动态加载图片

来源:互联网 发布:python判断字符串查找 编辑:程序博客网 时间:2024/05/17 03:04

Iphone在加载列表时,如果每个等待把所有列表中的数据都加载完在显示相关内容,如果列表中有一些比较大的图片,加载的时间比较长,那么给用户的效果就很差了,下面详细是一种实现动态加载图片的办法:

 

[cpp] view plaincopy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView  
  2.          cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
  3.     UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"tag"];  
  4.     if (cell==nil) {  
  5.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  6.                                        reuseIdentifier:@"tag"] autorelease];  
  7.     }     
  8.     //表格设计  
  9.     NSDictionary* one = [array objectAtIndex:indexPath.row];  
  10.     cell.textLabel.text = [one objectForKey:@"title"];  
  11.     cell.detailTextLabel.text = [one objectForKey:@"content"];  
  12.      [NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];  
  13.     return cell;  
  14. }  
  15.   
  16. - (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath  
  17. {  
  18.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  19.     UIImage *image = [self getImageForCellAtIndexPath:indexPath];  
  20.     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];  
  21.     [cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];  
  22.     [image release];  
  23.     [pool release];  
  24. }  
  25. -(UIImage *)getImageForCellAtIndexPath:(NSIndexPath *)indexPath  
  26. {  
  27.     id path = [[array objectAtIndex:indexPath.row] objectForKey:@"image"];  
  28.     NSURL *url = [NSURL URLWithString:path];  
  29.     NSData *data = [NSData dataWithContentsOfURL:url];  
  30.     UIImage *image = [[UIImage alloc] initWithData:data cache:NO];  
  31.     return image;  
  32. }  

0 0
原创粉丝点击