0

您好,我正在使用 UICollectionView 在我的 iphone 应用程序中显示图像,但是当我滚动视图时,加载的图像消失并再次加载图像,这是因为“dequeueReusableCellWithReuseIdentifier”,这是我的代码

static NSString * const kCellReuseIdentifier = @"collectionViewCell";
[self.collectionViewPack registerNib:[UINib nibWithNibName:@"CollectionViewItem" bundle:nil] forCellWithReuseIdentifier:kCellReuseIdentifier];

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

        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellReuseIdentifier forIndexPath:indexPath];

        cell.layer.shouldRasterize = YES;
        cell.layer.rasterizationScale = [UIScreen mainScreen].scale;

        UILabel *titleLabel = (UILabel *)[cell viewWithTag:100];
        UIImageView *icon=(UIImageView *)[cell viewWithTag:101];
        //    [titleLabel setText:[NSString stringWithFormat:@"%d",indexPath.row]];
        [titleLabel setText:[NSString stringWithFormat:@"%@",[arrayImages objectAtIndex:indexPath.row]]];
        icon.image =[UIImage imageNamed:@"loading-1.png"];


        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *imagePath = [NSString stringWithFormat:@"%@", [test.arrImages objectAtIndex:indexPath.row]];
            NSURL *imageUrl     = [NSURL URLWithString:imagePath];
            NSData *imageData   = [NSData dataWithContentsOfURL:imageUrl];
            UIImage *image      = nil;
            if (imageData){

                image = [[UIImage alloc] initWithData:imageData];
                icon.image = image;

            }

            [image release];

        });



        return cell;


    }

请帮我解决这个问题。

4

3 回答 3

3

我想您需要在代码中添加一些图像缓存,我建议您使用AFNetworking,您可以轻松做到:

import "UIImageView+AFNetworking.h"

……

[cell.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default"]];

而且,它有一个内部缓存可以帮助:D

对于您的代码...尝试将其添加到您的块中

dispatch_async(dispatch_get_main_queue(), ^{
    icon.image = image;
});
于 2013-09-12T14:47:15.350 回答
0
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
                 cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    // Setup cell identifier
    static NSString *cellIdentifier = @"put you viewController here";

    /* this block to use nib-based cells */
     UICollectionViewCell *cell = 
      [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier 
                                                  forIndexPath:indexPath];
    /* end of nib-based cell block */

    /* this block to use subclass-based cells */
  yourviewController *cell = 
  (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier 
                                                      forIndexPath:indexPath];
于 2013-09-12T13:47:37.500 回答
0

更改kCellReuseIdentifierdynamic

static NSString * const kCellReuseIdentifier = @"collectionViewCell";

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

    NSString * kCellReuseIdentifier = 
      [NSString stringWithFormat:@"collectionViewCell%d",indexPath.row];

它可能有效

于 2013-09-12T13:37:09.540 回答