【发布时间】:2015-07-14 23:49:59
【问题描述】:
我有一个UICollectionView,我在其中加载了多个图像。从我一直在阅读的内容来看,为了将正确的图像与每个单元格匹配,我需要将UIImageView 子类化并在那里获取图像。因为每次我collectionView reloadData时,都会有一些图像重复,而且它们都乱码了。但我不确定如何做到这一点,也没有找到任何教程。我正在使用 Parse 作为数据库。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
albumImageCell *cell = (albumImageCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[albumImageCell alloc]init];
}
PFObject *temp = [_dataArray objectAtIndex:indexPath.row];
PFFile *file = [temp objectForKey:@"imageThumbnail"];
if (![cell.hasImage isEqualToString:@"YES"]) {
dispatch_async(imageQueue, ^{
NSData *data = [file getData];
if (data) {
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^(void){
cell.imageView.image = image;
cell.hasImage = @"YES";
});
}
});
}
return cell;
}
【问题讨论】:
-
当
dispatch_async返回时,您分配数据的UICollectionViewCell可能已经被重新分配。
标签: ios objective-c asynchronous uicollectionview