Reputation: 3
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.
Upvotes: 0
Views: 5529
Reputation: 298
-(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];
Upvotes: 0
Reputation: 603
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
Upvotes: 0
Reputation: 1778
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;
});
Upvotes: 3