【发布时间】:2017-03-12 09:41:22
【问题描述】:
我有一个应用已经发布了好几年。它在 UICollectionView 中检索 RSS 提要。 cellForItemAtIndexPath 方法设置文本并调用数据源方法以从提要中指定的链接加载图像。如果不存在,它会加载网页数据并搜索<img> 标签以获取图像。一旦找到/加载图像,就会调用委托方法将图像添加到单元格中。 (下)
在 iOS 8 和 9 中运行时一切正常,但在 iOS 10 中运行时,可见单元格会在最初加载 RSS 提要时使用图像进行更新,但在滚动时没有添加图像,并且我从 cellForItemAtIndexPath 得到 NULL。
当我向后滚动时会显示图像,如果我将 reloadItemsAtIndexPaths 添加到 imageWasLoadedForStory 但 reloadItemsAtIndexPaths 会破坏性能,则会显示图像。
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// ...
if (story.thumbnail) {
[imageView setAlpha: 1.0];
imageView.image = story.thumbnail;
UIActivityIndicatorView *lActivity = (UIActivityIndicatorView *) [collectionView viewWithTag: 900];
[lActivity removeFromSuperview];
}else{
[story setDelegate: self];
[story findArticleImageForIndexPath: indexPath];
}
// ...
}
//delegate method. Called when image has been loaded for cell at specified indexpath
- (void) imageWasLoadedForStory: (RSSStory *) story forIndexPath: (NSIndexPath *) indexPath
{
//get cell
CustomCollectionViewCell *customCell = (id) [self.collectionview cellForItemAtIndexPath: indexPath];
NSLog(@"imageWasLoadedForStory row %i section %i and class %@", (int)indexPath.row, (int)indexPath.section, [customCell class]);
//if cell is visible ie: cell is not nil then update imageview
if (customCell) {
UIImageView *imageView = (UIImageView *) [customCell viewWithTag: 300];
imageView.image = story.thumbnail;
UIActivityIndicatorView *lActivity = (UIActivityIndicatorView *) [customCell viewWithTag: 900];
[lActivity removeFromSuperview];
[customCell setNeedsLayout];
[customCell setNeedsDisplay];
}
//[self.collectionview reloadItemsAtIndexPaths: [NSArray arrayWithObject: indexPath]];
}
- (void) findArticleImageForIndexPath: (NSIndexPath *) indexPath
{
//kick off image search
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.thumbnail = [self findArticleForStoryForIndexPath:indexPath];
dispatch_async( dispatch_get_main_queue(), ^{
//image set - return
[self.delegate imageWasLoadedForStory: self forIndexPath: indexPath];
});
});
}
【问题讨论】:
-
好吧,我可能没疯。我已经能够通过使用 tag 属性让它工作。在 cellForItemAtIndexPath 我添加了: [cell setTag: indexPath.row];并在我的委托方法中添加: CustomCollectionViewCell *customCell = (CustomCollectionViewCell *) [self.collectionview viewWithTag: indexPath.row];这使一切正常,但我认为这不是处理此问题的监管方式。
标签: ios uicollectionview uicollectionviewcell xcode8 ios10