0

我正在尝试为集合视图实现https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html 代码。一切正常。但是在重新加载旧单元格时的 cellForRowAtIndex 方法中,应用程序崩溃了。看看代码:

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:

(NSInteger)section
{
    if (self.galleryItemsArray) {
        return [self.galleryItemsArray count];
    }
    else
    {
        return 0;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    NSUInteger nodeCount = [self.galleryItemsArray count];

    static NSString* simpleTableIdentifier = @"SavedMessageCell_ID";

    SavedMessagesCell *cell = [cv dequeueReusableCellWithReuseIdentifier:simpleTableIdentifier forIndexPath:indexPath];
    if (cell == nil)
    {
        cell  = [[SavedMessagesCell alloc] initWithFrame:CGRectZero];

    }

    cell.textLabel.text = [NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section];

    if(nodeCount>0)
    {
        GalleryItemDS *item = [self.galleryItemsArray objectAtIndex:indexPath.row];
        // Only load cached images; defer new downloads until scrolling ends
        if (!item.imageOfItem)
        {
            if (gallery_collectionView.dragging == NO && gallery_collectionView.decelerating == NO)
            {
                [self startIconDownload:item forIndexPath:indexPath];
                NSLog(@"%@",item.PhotoPath);
            }
            // if a download is deferred or in progress, return a placeholder image
            cell.image.image = [UIImage imageNamed:@"Icon.png"];
        }
        else
        {
            NSLog(@"%@",[NSString stringWithFormat:@"{%ld,%ld}", (long)indexPath.row, (long)indexPath.section]);
            NSLog(@"%@",item.PhotoPath);

            **cell.image.image = item.imageOfItem; // Crashing here**

        }
    }

    return cell;

}



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

            SavedMessagesCell *cell = (SavedMessagesCell *)[gallery_collectionView cellForItemAtIndexPath:indexPath];

            // Display the newly loaded image
            cell.image.image = galleryItem.imageOfItem;
//            cell.backgroundColor = [UIColor colorWithPatternImage:galleryItem.imageOfItem];
            // Remove the IconDownloader from the in progress list.
            // This will result in it being deallocated.
            [self.imageDownloadsInProgress removeObjectForKey:indexPath];

        }];
        [self.imageDownloadsInProgress setObject:iconDownloader forKey:indexPath];
        [iconDownloader startDownload];
    }
}
- (void)loadImagesForOnscreenRows
{
    if ([self.galleryItemsArray count] > 0)
    {
        NSArray *visiblePaths = [gallery_collectionView indexPathsForVisibleItems];
        for (NSIndexPath *indexPath in visiblePaths)
        {
            GalleryItemDS *appRecord = [self.galleryItemsArray objectAtIndex:indexPath.row];

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


#pragma mark - UIScrollViewDelegate


// -------------------------------------------------------------------------------
//  scrollViewDidEndDragging:willDecelerate:
//  Load images for all onscreen rows when scrolling is finished.
// -------------------------------------------------------------------------------
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (!decelerate)
    {
        [self loadImagesForOnscreenRows];
    }
}

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

在此处输入图像描述

请帮助我。

4

0 回答 0