我有UICollectionView
一个自定义的实现UICollectionViewCell
。数据数组是从 Internet 获取的,但图像是在用户访问内容时下载的。
虽然这可能不是最有效的方法,而且我不打算这样,但它确实暴露了我正在努力解决的某个问题。似乎单元格正在显示“缓存”或“旧”图像,当我慢慢滚动集合视图时,单元格图像不断变化。
这可能是当时无法从网上获得实际单元格图像的问题,我的问题是如何强制空单元格或某些加载活动监视器而不显示不正确的图像?
在此先感谢,小齿轮
-(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;
}