簡體   English   中英

iOS 5:UITableView單元格不能平滑滾動

[英]iOS 5: UITableView cells are not scrolling smooth

使用:

  • iOS 5,ARC,StoryBoards。

每個單元格上都有1個圖像和文本。 滾動時圖像不會調整大小。 有兩個版本的images image.png和image@2x.png。

通過在故事板中使用UIImageView和UILabel來管理自定義單元格。

碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
Continent *continent=[self.items objectAtIndex:[indexPath row]];
ContinentCell *cell = (ContinentCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.continentName.text=continent.continentName;
    cell.textView.text=continent.countriesHash;
    cell.imageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:continent.continentImage ofType:@"png"]];
 return cell;
}

邪惡在哪里? 先感謝您。

[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:continent.continentImage ofType:@"png"]];

價格昂貴。 您希望在后台異步加載圖像,然后在准備好時將其顯示在主線程上。

這是一個非常粗略的解決方案

    cell.imageView.image = nil;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      UIImage * img = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:continent.continentImage ofType:@"png"]];
      dispatch_sync(dispatch_get_main_queue(), ^{
        cell.imageView.image = img;
      });
    });

查看拋光您的應用程序:提示和技巧,以提高響應度和性能視頻的WWDC 201126:48 他們已經准確地討論了你所面臨的問題。 不要錯過,這將是非常有幫助的..!

確保您的圖片不大,然后:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    Continent *continent=[self.items objectAtIndex:[indexPath row]];
    ContinentCell *cell = (ContinentCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil) cell = [[ContinentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
    cell.continentName.text = continent.continentName;
    cell.textView.text = continent.countriesHash;
    cell.imageView.image = [UIImage imageNamed:continent.continentImage];
    return cell;
}

我做了兩個重大改變:

  1. 新增if(cell == nil) [[ContinentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]因為我沒有找到初始化細胞(除非你使用的代碼-registerNib或其他超sectet功能,我可以”說因為它在NDA下

  2. 替換了imageWithContentsOfFile:...使用簡單的imageNamed ,因為你從主包加載圖像,如果圖像不大, -imageNamed緩存它,所以加載速度更快。 (和-imageNamed不需要文件擴展名)

- (UITableViewCell *)tableView:(UITableView *)tableView 
                                 cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    Continent *continent=[self.items objectAtIndex:[indexPath row]];
    ContinentCell *cell = (ContinentCell*)[tableView 
                               dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[ContinentCell alloc] 
                               initWithStyle:UITableViewCellStyleDefault 
                               reuseIdentifier:CellIdentifier];
     }
    cell.continentName.text=continent.continentName;
    cell.textView.text=continent.countriesHash;
    //cell.imageView.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
                        pathForResource:continent.continentImage ofType:@"png"]];
    cell.imageView.image = [UIImage cachedImage:continent.continentImage];
    return cell;
}

我建議你使用imageNamed而不是imageWithContentsOfFile。

imageNamed方法將圖像加載到緩存中 ,下次它將從緩存加載,其中imageWithContentsOfFile方法從指定的路徑加載圖像而沒有NO cahching,它將在內存中創建多個副本。

您可以創建自己的圖像緩存方法。 只需聲明NSMutableDictionary * imagedCacheDict

如果內存不足,可以通過 [imagedCacheDict removeAllObjects] 刪除所有對象

 - (UIImage*)cachedImage:(NSString*)fileName
{
   UIImage *cacheImage = [imagedCacheDict objectForKey:fileName];

   if (nil == cacheImage)
   {
      NSString *cacheImageFile = [NSString stringWithFormat:@"%@.png", 
                                   [[NSBundle mainBundle] resourcePath], fileName];
      cacheImage = [UIImage imageWithContentsOfFile:cacheImageFile];
      [imagedCacheDict setObject:cacheImage forKey:fileName];
   }
   return cacheImage;
}

因此,永遠不要使用imageNamed方法,它會通過消耗大量內存來顯示您的應用程序。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM