我有一个UICollectionView
实现与自定义UICollectionViewCell
。 数据的数组从互联网获取,但图像在用户访问内容时下载。GCD和细胞重用UICollectionView
虽然这可能不是最有效的方法,我不打算这样离开,但它确实暴露了我正在努力解决的某个问题。看起来这些单元格显示“缓存”或“旧”图像,当我缓慢滚动收集视图时,单元格图像不断变化。
这可能是一个问题,实际的细胞图像不可从网络在那个时刻和我的问题是我如何强制一个空单元格或一些加载活性监视器,而不是显示不正确的图像?
在此先感谢,小
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * CellIdentifier = @"Event";
EventCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
NSString *title = [[events objectAtIndex:indexPath.item] objectForKey:@"title"];
NSString *imageUrl = [[[events objectAtIndex:indexPath.item] objectForKey:@"photo"] objectForKey:@"url"];
dispatch_queue_t imageFetchQ = dispatch_queue_create("image fetched", NULL);
dispatch_async(imageFetchQ, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]];
if (imageData)
{
dispatch_async(dispatch_get_main_queue(), ^{
cell.eventImage.image = [UIImage imageWithData:imageData];
cell.eventTitle.text = title;
});
}
});
return cell;
}
我会建议你使用NSOperationQueue来代替,就像本教程中的内容: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues – 2013-04-29 08:12:04
我试过按照教程,但它并没有真正处理重用问题。我看到NSOperationQueue将适用于这种解决方案,但问题依然存在。我想我需要实现某种机制来验证哪个图像即将显示在每个单元格中。不知道如何。然而。 – Pinion 2013-05-02 23:23:43