2014-03-19 37 views
0

我的表格中有一个很慢的滚动条,滚动条具有从网页加载并调整大小,但图像已加载的图像,所以我不明白为什么滚动速度很慢。 我已阅读并试图slow scrolling of UITableView没有成功(我看空细胞)在表格视图中慢速滚动

这是细胞(它也有编码区冠军)

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


    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    NSString *[email protected]""; 
    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; 
} 
+0

您应该使用其他'/ if'(三个'if'测试consecutives)。你也可以看看延迟加载(加载你的URL图像)。 – Larme

回答

1

首先,图标的URL是什么?它是你已经下载的本地图片的文件网址吗?如果没有,您想要在后台线程下载并将其缓存到本地,然后在此处使用该本地文件URL。请注意,当文件在后台线程中下载时,您不想重新加载整个表格!只需上传与该图像相关的一个单元格(或者甚至更好一个imageView)。否则,你会重新加载表格,造成其他问题。

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

而且,虽然一件小事情,改变你的三个如果是if/else ifs。你正在检查相同的值,所以你不需要检查三次。更改订单以便首先检查最受欢迎的选项也可以节省您一些比较。

更新:

你可以做,使这个更快的另一件事,是该单元配置一次。你每次都要设置字体,颜色等。如果将其移动到单元的子类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; 
} 
+0

我不明白,这种方法被调用一次,即使它需要时间,在某个时间点,所有图像应该已经可以使用,所以为什么它保持缓慢?它一次又一次加载图像吗?这个函数是不是被调用过一次? – Curnelious

+0

不,这种方法每次出现在屏幕上时都会调用。如果它滚动并重新打开,它会再次调用它。 –

1

不要让代码在细胞加载形象,这会让你放慢脚步。 一般情况下,你要么预处理中的图像下载,使他们可立即在小区创建方法或更改代码在单元格中在后台线程加载图像,使它们撑不起来的细胞图。

查看表视图编程指南和从苹果块编程指南。对不起,我正在打电话给我的简短回答:)

1

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

为了避免这个问题,你可以使用AFNetworking,为UIImageView是加载在异步模式下的图像,或使用SDWebImage具有相同的功能有很大的类别,也可以写你的NSURLSession实现加载内容。