0


由于我是 iPad 应用程序开发的新手,请帮助我解决简单的问题。
我想在服务器的 tableview 中显示图像。它显示正确。
但是当我向上滚动表格并再次回到那里时,它将再次从服务器下载图像数据。
请帮我。

编辑:
表视图:cellForRowAtIndexPath:

static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 75)];
[imgView setImage:[UIImage imageNamed:@"strip_normal.png"]];
[imgView setHighlightedImage:[UIImage imageNamed:@"strip_hover.png"]];
[cell addSubview:imgView];
[imgView release];

UIImageView *imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 64, 64)];
imgView1.tag = indexPath.row;
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];
[imgView1 setImage:img];
[cell addSubview:imgView1];
[imgView1 release];

return cell;

提前致谢。

4

1 回答 1

1

你的问题是这条线

UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abc.com/abc/%@.png", [[arrMyCourses objectAtIndex:indexPath.row] valueForKey:@"CourseNumber"]]]]];

每次表格视图请求单元格时,您都在下载图像,相信我这种情况经常发生,因为表格视图不会一次构建整个视图。它重用离开屏幕的单元格来呈现新的可见单元格。因此,一旦一个单元格离开屏幕并重新打开,cellForRowAtIndexPath:就会再次调用 。所以图像被再次下载。您还将同步下载图像,这也会阻止 UI。

要解决此问题,您应该考虑在开始时下载它们一次并将它们保存在本地临时位置,并在必要时将它们加载到内存中。将它们全部保存在内存中可能会很昂贵。此外,使用performSelectorInBackground:withObject. 您必须将 UIKit 更新发送回主线程,否则您将遇到崩溃。

于 2011-06-03T05:16:58.157 回答