只有少量图像的生涩滚动

时间:2010-11-18 01:53:48

标签: iphone uitableview scroll

我正在使用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];
}

1 个答案:

答案 0 :(得分:1)

看起来每次配置单元格时,都会从网上重新下载整个图像(或者从硬盘读取,如果这是您的URL指向的位置):

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

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

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

相反,您有两种选择:

  1. 首先下载并存储所有图像(如果只有12个,可能不会占用太多内存)。

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