2012-05-28 62 views
0

我正在爲延遲加載實施一個示例代碼。 我有一個陣列,它具有10url的圖像: - //自定義表格視圖單元格的外觀。Lazyloading in iphone +無法顯示下載的圖像

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; 

    if (!iconDownloader.ImageDispaly) 
    { 
     if (Tble.dragging == NO && Tble.decelerating == NO) 
     { 
      [self startIconDownload:[ArrayImage objectAtIndex:indexPath.row] forIndexPath:indexPath]; 
     } 

     //cell.imgView.image = [UIImage imageNamed:@"event_image.png"]; 
     cell.imageView.image=[UIImage imageNamed:@"default-image.png"]; 
     NSLog(@"image not download"); 
    } 
    else 
    { 
     //cell.imgView.image=[aTweet objectForKey:@"image"]; 
     cell.imageView.image=iconDownloader.ImageDispaly.image; 

    } 

    return cell; 
} 


#pragma mark - 
#pragma mark Table cell image support 
- (void)startIconDownload:(NSString *)str forIndexPath:(NSIndexPath *)indexPath 
{ 
    //[imageDownloadsInProgress retain]; 
    iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; 
    if (iconDownloader == nil) 
    { 
     iconDownloader = [[IconDownloader alloc] init]; 
     // iconDownloader.appRecord = aTweet; 
     iconDownloader.imageurlstring=[ArrayImage objectAtIndex:indexPath.row]; 
     iconDownloader.indexPathInTableView = indexPath; 
     iconDownloader.delegate = self; 
     [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath]; 
     [iconDownloader startDownload]; 
     [imageDownloadsInProgress retain]; 
     [iconDownloader release]; 
    } 
} 

// this method is used in case the user scrolled into a set of cells that don't have their app icons yet 
- (void)loadImagesForOnscreenRows 
{ 
    if ([ArrayImage count] > 0) 
    { 
     NSArray *visiblePaths = [Tble indexPathsForVisibleRows]; 
     for (NSIndexPath *indexPath in visiblePaths) 
     { 
      iconDownloader =[imageDownloadsInProgress objectForKey:indexPath]; 

      if (!iconDownloader.ImageDispaly) // avoid the app icon download if the app already has an icon 
      { 
       [self startIconDownload:[ArrayImage objectAtIndex:indexPath.row] forIndexPath:indexPath]; 
      } 
     } 
    } 
} 

- (void)appImageDidLoad:(NSIndexPath *)indexPath 
{ 
    iconDownloader =[imageDownloadsInProgress objectForKey:indexPath]; 
    if (iconDownloader != nil) 
    { 

     UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(10,5,60,60)]; 
     UITableViewCell *cell = [Tble cellForRowAtIndexPath:iconDownloader.indexPathInTableView]; 
     // [cell.btnImage setBackgroundImage:[iconDownloader.appRecord valueForKey:@"image"] forState:UIControlStateNormal]; 
     image.image=iconDownloader.ImageDispaly.image; 
     cell.imageView.image=image.image; 

     NSLog(@"%@ ======%@",cell.imageView.image,image.image); 

    } 

} 

這是我得到我的控制檯 的NSLog上(@ 「%@ ==%@」,cell.imageView.image,image.image); 在執行應用程序

2012-05-28 16:41:44.998 Lazyloading[97195:207] <UIImage: 0x4b56300> ======<UIImage: 0x4b56300> 
2012-05-28 16:41:44.999 Lazyloading[97195:207] <UIImage: 0x4e24f00> ======<UIImage: 0x4e24f00> 
2012-05-28 16:41:45.000 Lazyloading[97195:207] <UIImage: 0x4b55df0> ======<UIImage: 0x4b55df0> 
2012-05-28 16:41:45.001 Lazyloading[97195:207] <UIImage: 0x4b406e0> ======<UIImage: 0x4b406e0> 
2012-05-28 16:41:45.260 Lazyloading[97195:207] <UIImage: 0x4e2f9f0> ======<UIImage: 0x4e2f9f0> 
2012-05-28 16:41:45.263 Lazyloading[97195:207] <UIImage: 0x4e0a2e0> ======<UIImage: 0x4e0a2e0> 
2012-05-28 16:41:45.294 Lazyloading[97195:207] <UIImage: 0x4b40180> ======<UIImage: 0x4b40180> 

但不被顯示在tableview中的圖像.. 任何機構可以請大家幫我出這個..

+0

你沒有按照演示提供正確地在懶惰加載蘋果參考[this](https://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html)正確鏈接。 –

回答

0

嘗試調用:

[cell setNeedsDisplay]; 

在哪裏你的NSLog線是:

NSLog(@"%@ ======%@",cell.imageView.image,image.image); 
+0

我已經這樣做了,但它仍然沒有顯示圖像 –

+0

奇怪...創建imageView並將圖像分配給它的代碼看起來多餘,但不是問題。爲什麼不只是這樣:'UITableViewCell * cell = [Tble cellForRowAtIndexPath:iconDownloader.indexPathInTableView]; cell.imageView.image = iconDownloader.ImageDispaly.image;'。我假設ImageDispaly是一個錯字,但保留了它。您是否檢查了下載圖像中的寬度/高度和其他信息以確保其正確。 – Diziet