uicollectionview可重复使用的单元格图像在滚动时重新加载

时间:2013-09-12 13:31:51

标签: iphone ios objective-c uicollectionview uicollectionviewcell

你好我正在使用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;


    }

请帮助我解决这个问题。

3 个答案:

答案 0 :(得分:3)

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

import "UIImageView+AFNetworking.h"

...

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

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

并为您的代码...尝试在您的块中添加此

dispatch_async(dispatch_get_main_queue(), ^{
    icon.image = image;
});

答案 1 :(得分:0)

kCellReuseIdentifier更改为dynamic

static NSString * const kCellReuseIdentifier = @"collectionViewCell";

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

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

它可能有用

答案 2 :(得分: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];