目标C语言 在表格视图中滚动缓慢



我有一个缓慢的滚动在我的表,滚动条有图像,从网页加载和调整大小,但图像已经加载,所以我不明白为什么滚动是缓慢的。我已经阅读并尝试缓慢滚动UITableView没有成功(我看到空单元格)

这是单元格(它也有分段标题编码)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    NSString *data=@"";
    NSString *icon;
            NSMutableArray *result=[allResults objectAtIndex:indexPath.section];
            NSDictionary *dic=[result objectAtIndex:indexPath.row+1];
            if([[result objectAtIndex:0] isEqualToString:@"types"])
            {
                NSString *title=[dic objectForKey:@"title"];
                icon=[dic objectForKey:@"icon"];
                data=[data stringByAppendingString:[NSString stringWithFormat:@"%@",title]];
            }
            if([[result objectAtIndex:0] isEqualToString:@"subServices"])
            {
                NSString *title=[dic objectForKey:@"title"];
                icon=[dic objectForKey:@"icon"];
                     data=[data stringByAppendingString:[NSString stringWithFormat:@"%@",title]];
            }
            if([[result objectAtIndex:0] isEqualToString:@"businesses"])
            {
                NSString *title=[dic objectForKey:@"title"];
                icon=[dic objectForKey:@"logo"];
                     data=[data stringByAppendingString:[NSString stringWithFormat:@"%@",title]];
            }
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.text = data; 
    cell.textLabel.textColor=[UIColor blackColor];
    cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:22];
    cell.textLabel.textColor=[UIColor colorWithRed:122.0/255.0 green:181.0/255.0 blue:196.0/255.0 alpha:1];
    cell.imageView.layer.masksToBounds = YES;

    //load image
    NSURL *url = [NSURL URLWithString:icon];
    NSData *imdata = [NSData dataWithContentsOfURL:url];
    UIImage *logo=[UIImage imageWithData:imdata scale:1];
    UIImage *scaled=[self  resizeImage:logo imageSize:CGSizeMake(30, 30)];
    cell.imageView.image=scaled ;
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 12.0;


    return cell;
}

首先,图标的URL是什么?它是您已经下载的本地图像的文件URL吗?如果不是,你想在后台线程中下载它并将其缓存到本地某个地方,然后在这里使用本地文件URL。注意,当文件在后台线程中下载时,您不希望重新加载整个表!只需上传与该图像相关的一个单元格(或者更好的是一个imageView)。否则,您将重新加载表的次数多得离谱,这将导致其他问题。

接下来,您将在每次调用时调整图像的大小。您应该调整一次图像大小并缓存结果。如果您只在这个位置使用图像,请在下载时调整图像的大小,并只缓存调整后的版本。如果在另一个视图中使用,则缓存原始版本和调整大小的版本。

还有,把三个if改成if/else if,这是一件小事。你检查的是同一个值,所以你不需要检查三次。改变顺序,让最受欢迎的选项先被选中,也会节省你一些比较。

更新:

您可以做的另一件事是使这个更快,配置单元格一次。你每次都要设置字体、颜色等。如果你把它移到单元格的子类init方法中,它就不需要一遍又一遍地调用它了。

同时,有一些你正在做的事情是你不需要做的。检查更新后的版本,注意:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString* CellIdentifier = @"Cell";
    UITableViewCell* cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier
                                                                 forIndexPath:indexPath];

    // Move this part to the init method of a subclass of UITableViewCell
    cell.textLabel.numberOfLines = 1;
    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold"
                                          size:22];
    cell.textLabel.textColor = [UIColor colorWithRed:122.0 / 255.0
                                               green:181.0 / 255.0
                                                blue:196.0 / 255.0
                                               alpha:1];
    cell.imageView.layer.masksToBounds = YES;
    cell.imageView.layer.cornerRadius = 12.0;
    // End of section to move to init methods
    NSString* icon = nil;
    NSMutableArray* result = [allResults objectAtIndex:indexPath.section];
    NSDictionary* dic = [result objectAtIndex:indexPath.row + 1];
    if ([[result objectAtIndex:0] isEqualToString:@"types"]) {
        icon = [dic objectForKey:@"icon"];
    }
    else if ([[result objectAtIndex:0] isEqualToString:@"subServices"]) {
        icon = [dic objectForKey:@"icon"];
    }
    else if ([[result objectAtIndex:0] isEqualToString:@"businesses"]) {
        icon = [dic objectForKey:@"logo"];
    }
    cell.textLabel.text = [dic objectForKey:@"title"];
    // Move the loading of the URL to a background thread if the url is not a local file URL
    //load image
    NSURL* url = [NSURL URLWithString:icon];
    NSData* imdata = [NSData dataWithContentsOfURL:url];
    UIImage* logo = [UIImage imageWithData:imdata
                                     scale:1];
    // Move the resizing of the image to however you load the image from the network,
    // resize it once and cache the results, load the cached version only
    UIImage* scaled = [self resizeImage:logo
                              imageSize:CGSizeMake(30, 30)];
    cell.imageView.image = scaled;
    return cell;
}

单元格中不要有加载图像的代码,这会减慢你的速度。一般来说,你要么对图像下载进行预处理,这样它们就可以在单元格创建方法中立即可用,要么改变单元格中的代码,在后台线程中加载图像,这样它们就不会妨碍单元格绘制。

查看apple的表视图编程指南和块编程指南。很抱歉,我正在用手机打字。

显示@Andrey Chernukha在他的回答中你同步下载图像,这导致滚动缓慢。

为了避免这个问题,你可以使用AFNetworking,它对UIImageView有很大的分类,以异步模式加载图像,或者使用具有相同功能的SDWebImage,或者你可以编写你的NSURLSession实现来加载内容。

最新更新