sanjeet
sanjeet

Reputation: 1508

reload issue with UICollectionView iOS SDK

hey guys i displaying images from web services, now my proble is i am getting some strange issue with no idea

here is my code snippet

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


CustomCell *cell = (CustomCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CustomCell" forIndexPath:indexPath];

NSString *strurl=[[arrUrlImage objectAtIndex:indexPath.row] objectAtIndex:0];

NSURL *url = [NSURL URLWithString:strurl];


ImageRequest *request = [[ImageRequest alloc] initWithURL:url];
cell.imageView.image=[UIImage imageNamed:@"purple.jpg"];
UIImage *image = [request cachedResult];
if (image) { 

    cell.imageView.image=image;       

} else {
    [request startWithCompletion:^(UIImage *image, NSError *error) {
        if (image && [[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {






            @try
            {
                [_collectionView performBatchUpdates:^{                       

                    [_collectionView reloadItemsAtIndexPaths:@[indexPath]];
                } completion:nil];

            }
            @catch (NSException *except)
            {                    
                NSLog(@"%@", except.description);
            }

        }
    }];
}


if (indexPath.row==arrUrlImage.count-1) {
    NSLog(@"load more image.");
    NSString *nextPageUrl=[[arrMedia valueForKey:@"pagination"] valueForKey:@"next_url"];
    [arrMedia removeAllObjects];
    NSLog(@"%@",nextPageUrl);
    ShowNetworkActivityIndicator();
    [self grabMedia:nextPageUrl];     

}

return cell;  
}

i am getting exception with this error msg

Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit_Sim/UIKit-2903.23/UICollectionView.m:3716

and the exception log: Invalid update: invalid number of items in section 0. The number of items contained in an existing section after the update (80) must be equal to the number of items contained in that section before the update (60), plus or minus the number of items inserted or deleted from that section (1 inserted, 1 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).

any help appriciated.

Upvotes: 1

Views: 372

Answers (1)

rishi
rishi

Reputation: 11839

You are calling reloadItemsAtIndexPaths, while cells are actually not present, so you need to use insertItemsAtIndexPaths.

[_collectionView performBatchUpdates:^{                       

                [_collectionView insertItemsAtIndexPaths:@[indexPath]];
            } completion:nil];

Upvotes: 0

Related Questions