Question

in iOS for iPhone app, i have to make one grid so i am using UICollectionView. the Problem is when i scroll down, then it automatically repeats data and make more of time.how to stop data add automatically, and make data load only one time. Here is my code

in viewDidLoad :
 [self.collectionView registerClass:[CVCell class] forCellWithReuseIdentifier:@"cvCell"]; 

-(void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];


}


// add Grid

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

        return [self.setFacebookimage count];
   }

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   return [setFacebookimage count];


}

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

    // Setup cell identifier
    static NSString *cellIdentifier = @"cvCell";

    CVCell *cell = (CVCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    // by nishant set Grid image
    if([[setFacebookimage objectAtIndex:indexPath.row] isEqualToString:@"img19.png" ]){
        cell.cellimageview.image=[UIImage imageNamed:@"img19.png"];
    }else{

        NSData *imageDatafacebookGrid = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: [setFacebookimage objectAtIndex:indexPath.row]]];
        // NSLog(@"%@",imageDatafacebook);
        cell.cellimageview.image=[UIImage imageWithData:imageDatafacebookGrid ];
    }


    return cell;

}
- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // Adjust cell size for orientation
    if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
        return CGSizeMake(100.f, 100.f);
    }
    return CGSizeMake(150.f, 150.f);
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
       // NSLog(@"End >>>>> End ");
}

in my facebook array, i have 5 data. how to solve this problem

Was it helpful?

Solution

You can save Images in NSCache. so that it won't refresh your Cell each time while you scroll and it can increase your scroll speed rate. Using NSCache you can check for the image if it is available or not. if no you can download and save in NSCache.

declare NSCahe *imageCache;

Then in cellForItemAtIndexPath try the below Code Snippet :

if (cell.cellimageview.image == nil) {
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString: [setFacebookimage objectAtIndex:indexPath.row]]];
    UIImage *image = [UIImage imageWithData:data];
    [imageCache setObject:image forKey:[setFacebookimage objectAtIndex:indexPath.row]];

    dispatch_async(dispatch_get_main_queue(), ^{
        cell.cellimageview.image = image;
    });
});
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top