0

hello am using UICollectionView for showing images in my iphone application but when i scrolling the view the loaded images are gone and loading images again this is because of "dequeueReusableCellWithReuseIdentifier" and this is my code

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;


    }

please help me out from this.

ANIL1207
  • 3
  • 1
  • 2
  • Check [this answer](http://stackoverflow.com/a/9188019/1407017). It is for `UITableViewCell` lazy loading images but same can be applied for `UICollectionViewCell`. – Amar Sep 12 '13 at 13:50

3 Answers3

3

I guess you need to add some image cache to your code, I recommend you to use AFNetworking, you can easily do:

import "UIImageView+AFNetworking.h"

......

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

And, it has an internal Cache that will help :D

and for your code... try by adding this in your block

dispatch_async(dispatch_get_main_queue(), ^{
    icon.image = image;
});
Camo
  • 1,778
  • 14
  • 12
  • You should really avoid using `dispatch_async` to fix issues in your code unless you're dealing with an Apple bug. Generally, this introduces technical debt and unnecessary complexity. – Zorayr Jan 06 '15 at 05:28
0

change kCellReuseIdentifier to dynamic

static NSString * const kCellReuseIdentifier = @"collectionViewCell";

to

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

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

It may work

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
sabeer
  • 603
  • 10
  • 28
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];