【发布时间】:2015-10-29 09:11:00
【问题描述】:
我正在制作一个简单的图库 iOS 程序,并且对 UICollectionView 数据源行为有疑问。
在我的代码中一切正常,除了每个单元格的图像在我向上或向下滚动时闪烁。
不可见单元格在可见之前不会下载图像。 在我的代码中,它是按需下载的。这是最佳做法吗?这就是我得到闪烁单元格的原因吗?
单元的可重用性让我感到困惑。在我的观察中,当一个细胞变得不可见时,它就会失去自己的形象。我怎样才能防止这种情况?这也是我得到闪烁单元格的原因吗?
这里是代码。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
FBPhotoCollectionViewCell *cell = (FBPhotoCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];
__weak FBPhotosVC *weakSelf = self;
if ([_PhotoLinks count] > 0) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL* theURL = [NSURL URLWithString:[_PhotoLinks objectAtIndex:(int)indexPath.row]];
UIImage* tempImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:theURL]];
if ([weakSelf.theCollectionView.indexPathsForVisibleItems containsObject:indexPath]) {
if (tempImage)
{
dispatch_async(dispatch_get_main_queue(), ^{
FBPhotoCollectionViewCell *cell = (FBPhotoCollectionViewCell *)[weakSelf.theCollectionView cellForItemAtIndexPath:indexPath];
cell.theImageView.image = tempImage;
});
}
else
{
// Download failed.
NSLog(@"Downloaded image is nil.");
}
}
});
}
cell.backgroundColor = [UIColor grayColor]; <br>
return cell; <br>
}
【问题讨论】:
标签: ios uicollectionview gallery