2010-11-18 50 views
2

我使用Apple在表中使用子视图显示的方法(大部分是从他们的文档)。我只通过一个rss feed拉入大约12张图像,但是这导致了滚动缓慢 - 如果我摆脱了图像,它会平稳移动。图像不是很大,所以不能成为问题。在研究更多涉及的解决方案(后台处理等)之前,有什么我可以做的,以使这项工作更好?只有少量图像的生涩滚动

感谢您给予的任何帮助。

#define MAINLABEL_TAG 1 
#define SECONDLABEL_TAG 2 
#define PHOTO_TAG  3 

-(UITableViewCell *)tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath { 
    UILabel * mainLabel, * secondLabel; 
    UIImageView * photo; 
    static NSString * CellIdentifier = @ "Cell"; 
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 

     mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(100.0, 0.0, 210.0, 0.0)] autorelease]; 
     mainLabel.tag = MAINLABEL_TAG; 
     mainLabel.font = [UIFont systemFontOfSize:14.0]; 
     mainLabel.textAlignment = UITextAlignmentRight; 
     mainLabel.textColor = [UIColor blackColor]; 
     mainLabel.opaque = YES; 
     mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview : mainLabel]; 

     secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(90.0, 30.0, 220.0, 0.0)] autorelease]; 
     secondLabel.tag = SECONDLABEL_TAG; 
     secondLabel.font = [UIFont systemFontOfSize:12.0]; 
     secondLabel.textAlignment = UITextAlignmentRight; 
     secondLabel.textColor = [UIColor darkGrayColor]; 
     secondLabel.opaque = YES; 
     secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview : secondLabel]; 

     photo = [[[UIImageView alloc] initWithFrame:CGRectMake(30.0, 3.0, 50.0, 40.0)] autorelease]; 
     photo.tag = PHOTO_TAG; 
     photo.opaque = YES; 
     photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [cell.contentView addSubview : photo]; 
    } else { 
     mainLabel = (UILabel *)[cell.contentView viewWithTag : MAINLABEL_TAG]; 
     secondLabel = (UILabel *)[cell.contentView viewWithTag : SECONDLABEL_TAG]; 
     photo = (UIImageView *)[cell.contentView viewWithTag : PHOTO_TAG]; 
    } 
    // Configure the cell. 
    mainLabel.text = [[items objectAtIndex:indexPath.row] objectForKey:@ "title"]; 
    secondLabel.text = [[items objectAtIndex:indexPath.row] objectForKey:@ "teacher"]; 
    NSString * path = [[items objectAtIndex:indexPath.row] objectForKey:@ "audimage"]; 
    NSString * mypath = [path stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    NSURL * url = [NSURL URLWithString:mypath]; 
    NSData * data = [NSData dataWithContentsOfURL:url]; 
    UIImage * img = [[UIImage alloc] initWithData:data]; 
    photo.image = img; 
    return cell; 
    [cell release]; 
} 
+0

'[细胞释放]'将永远不会被调用,因为没有什么方法返回后,将达到 – vikingosegundo 2010-11-18 02:12:09

回答

1

它看起来就像每一个你配置一个小区的时候,你再从网上下载整个图像(或者从硬盘读取,如果这就是你的URL指向):

NSData *data = [NSData dataWithContentsOfURL:url]; 
UIImage *img = [[UIImage alloc] initWithData:data]; 

这真的很慢! UITableView的工作方式是通过重复使用单元格 - 每次单元格离开屏幕时,可能会返回cellForRowAtIndexPath方法,以用作可见的下一个单元格。

这意味着每隔时间一个新的单元格变得可见,您正在下载并创建它的图像。

相反,你有两个选择:

  1. 下载并首先存储所有的图像(如果只有12,它可能不会采取过多的内存)。

  2. 根据需要下载图像,如果出现内存警告,请转储一些缓存的图像。这实施起来有点困难,所以我会先尝试第一个解决方案。

+0

Hmm..thanks..I想的that..so没有缓存与回事当图像滚动视图时,图像保持在内存中? – Allen 2010-11-18 02:09:51

+0

@艾伦,不 - 因为那个滚动视图的单元格会被分配一个新的图像,所以它会丢失旧的图像。 – 2010-11-18 02:15:04

+0

哦...所以他们现在的方式就像检查它是否失去了图像,然后才去再次获取图像..如果照片,图像是零,再次获取图像。对不起,如果这没有意义,只是试图找到简单的东西如果这甚至可能) – Allen 2010-11-18 02:19:11