Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone uicollection视图可重用单元格图像在滚动时正在重新加载_Iphone_Ios_Objective C_Uicollectionview_Uicollectionviewcell - Fatal编程技术网

Iphone uicollection视图可重用单元格图像在滚动时正在重新加载

Iphone uicollection视图可重用单元格图像在滚动时正在重新加载,iphone,ios,objective-c,uicollectionview,uicollectionviewcell,Iphone,Ios,Objective C,Uicollectionview,Uicollectionviewcell,您好,我使用UICollectionView在我的iphone应用程序中显示图像,但当我滚动视图时,加载的图像将消失,并再次加载图像,这是因为“dequeueReusableCellWithReuseIdentifier” 这是我的密码 static NSString * const kCellReuseIdentifier = @"collectionViewCell"; [self.collectionViewPack registerNib:[UINib nibWithNibName:@"

您好,我使用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;


    }

请帮我解决这个问题。

kCellReuseIdentifier
更改为
dynamic

static NSString * const kCellReuseIdentifier = @"collectionViewCell";


它可以工作

kCellReuseIdentifier
更改为
动态

static NSString * const kCellReuseIdentifier = @"collectionViewCell";
-(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];


它可能会工作

我想您需要在代码中添加一些图像缓存,我建议您使用,
-(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];
您可以轻松地执行以下操作:

导入“UIImageView+AFNetworking.h”

[cell.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default"]];
而且,它有一个内部缓存,这将有助于:D

对于你的代码。。。尝试在你的块中添加这个

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

我想您需要在代码中添加一些图像缓存,我建议您使用, 您可以轻松地执行以下操作:

导入“UIImageView+AFNetworking.h”

[cell.imageView setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"default"]];
而且,它有一个内部缓存,这将有助于:D

对于你的代码。。。尝试在你的块中添加这个

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

检查。它适用于
UITableViewCell
延迟加载图像,但同样适用于
UICollectionViewCell
。请检查。它适用于
UITableViewCell
延迟加载图像,但同样适用于
UICollectionViewCell
。您应该避免使用
dispatch\u async
来修复代码中的问题,除非您正在处理Apple bug。一般来说,这会带来技术负担和不必要的复杂性。除非您正在处理Apple bug,否则您应该真正避免使用
dispatch\u async
来修复代码中的问题。通常,这会带来技术债务和不必要的复杂性。