您好,我正在使用 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;
}
请帮我解决这个问题。