我有一个应用程序已经成功运行了几年。 它在一个UICollectionView中检索RSS Feed。 cellForItemAtIndexPath方法设置文本并调用数据源方法从链接中指定的Feed加载图像。 如果不存在,则加载网页数据并搜索<img>标签以获取图像。 找到/加载图像后,委托方法将被调用以将图像添加到单元格中。 (如下)
在iOS 8和9上运行时一切都正常,但是在iOS 10上运行时,在加载RSS feed时可见单元格会更新图片,但是在滚动时不会添加任何图片,并且我会从cellForItemAtIndexPath获得NULL。
当我向后滚动时显示图像,如果我在imageWasLoadedForStory中添加reloadItemsAtIndexPaths,则显示图像,但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];
});
});
}