2014-02-17 87 views
0

我試過搜索,但沒有找到任何有用的解決方案。只有當UITableViewcell在屏幕外時,圖標下載纔會下載圖像.iPhone

我正在使用以下代碼進行圖標延遲加載。 問題是,圖標是通過延遲加載下載的,但只有在特定單元格不在屏幕上並且滾動回屏幕時纔會看到圖標。

我認爲這是dequeueReusableCellWithIdentifier的問題,但不知道如何解決它。 圖像可以正常下載,但只有在單元格離開屏幕後才能在單元格中看到。

// ------------------------------------------------------------------------------- 
// tableView:cellForRowAtIndexPath: 
// ------------------------------------------------------------------------------- 
- (UITableViewCell *)tableView:(UITableView *)tableVw cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // customize the appearance of table view cells 
    // 
static NSString *CellIdentifier = @"LazyTableCell"; 
static NSString *PlaceholderCellIdentifier = @"PlaceholderCell"; 

// add a placeholder cell while waiting on table data 
NSUInteger nodeCount = [dataArray count]; 

if (nodeCount == 0 && indexPath.row == 0) 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier]; 

    cell.detailTextLabel.text = @"Loading…"; 

    return cell; 
} 


UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (!cell) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
} 


cell.backgroundColor = [UIColor grayColor]; 

// Leave cells empty if there's no data yet 
if (nodeCount > 0) 
{ 
    // Set up the cell... 
    AppRecord *appRecord = [dataArray objectAtIndex:indexPath.row]; 

    cell.textLabel.text = appRecord.appName; 
    cell.detailTextLabel.text = appRecord.artist; 

    // Only load cached images; defer new downloads until scrolling ends 
    if (!appRecord.appIcon) 
    { 
     if (tableView.dragging == NO && tableView.decelerating == NO) 
     { 
      [self startIconDownload:appRecord forIndexPath:indexPath]; 
     } 
     // if a download is deferred or in progress, return a placeholder image 
     cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"]; 
    } 
    else 
    { 
     cell.imageView.image = appRecord.appIcon; 
    } 

} 

return cell; 
} 


- (void)startIconDownload:(AppRecord *)appRecord forIndexPath:(NSIndexPath *)indexPath 
{ 
IconDownloader *iconDownloader = [imageDownloadsInProgress objectForKey:indexPath]; 
if (iconDownloader == nil) 
{ 
    iconDownloader = [[IconDownloader alloc] init]; 
    iconDownloader.appRecord = appRecord; 
    [iconDownloader setCompletionHandler:^{ 

     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; 

     // Display the newly loaded image 
     cell.imageView.image = appRecord.appIcon; 

     // Remove the IconDownloader from the in progress list. 
     // This will result in it being deallocated. 
     [imageDownloadsInProgress removeObjectForKey:indexPath]; 

    }]; 
    [imageDownloadsInProgress setObject:iconDownloader forKey:indexPath]; 
    [iconDownloader startDownload]; 
} 
} 


- (void)loadImagesForOnscreenRows 
{ 
if ([dataArray count] > 0) 
{ 
    NSArray *visiblePaths = [tableView indexPathsForVisibleRows]; 
    for (NSIndexPath *indexPath in visiblePaths) 
    { 
     AppRecord *appRecord = [dataArray objectAtIndex:indexPath.row]; 

     if (!appRecord.appIcon) 
      // Avoid the app icon download if the app already has an icon 
     { 
      [self startIconDownload:appRecord forIndexPath:indexPath]; 
     } 
    } 
} 
} 

#pragma mark - UIScrollViewDelegate 


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate 
{ 
if (!decelerate) 
{ 
    [self loadImagesForOnscreenRows]; 
} 
} 


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 
{ 
    [self loadImagesForOnscreenRows]; 
} 
+1

我覺得懶加載[SDWebImage(https://github.com/rs/SDWebImage)是最好的一個。在許多項目中爲我工作。 – cjd

+0

是的,我也實現了SDWebmage。但它給出了同樣的問題。像cell.imageview.image只在單元格滾動出屏幕時加載。有什麼幫助嗎? – ScarletWitch

回答

0

我做這樣的代碼下面,

SDWebImageManager *manager = [SDWebImageManager sharedManager]; 
[manager downloadWithURL:aURL 
       options:0 
       progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) 
{ 
    if (image) 
     [aCell.imgViewThumb setImage:image]; 

    else 
     [aCell.imgViewThumb setImage:[UIImage imageNamed:@"Dummy-image.jpg"]]; 

    [aCell.indicator stopAnimating]; 
}]; 
相關問題