質問

I tried searching but did not find any solutions helpful.

I am using the following code for icons lazy loading. The issue is, the icons are downloaded via lazy loading, but they are only seen once that particular cell is out of screen and is scrolled back into the screen.

I think it is some issue with dequeueReusableCellWithIdentifier but am not sure how to resolve it. The images are downloaded alright, but are only visible in the cell once the cell goes out of screen.

// -------------------------------------------------------------------------------
//  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];
}
役に立ちましたか?

解決

I did code like following,

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];
 }];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top