UIcollectionView cellForItemAtIndexPath仅在iOS 10中返回Null.在iOS 9和iOS 8中正常运行 [英] UIcollectionView cellForItemAtIndexPath returns Null in iOS 10 only. Works fine in iOS 9 and iOS 8

查看:74
本文介绍了UIcollectionView cellForItemAtIndexPath仅在iOS 10中返回Null.在iOS 9和iOS 8中正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个愉快地发货了两年的应用程序.它在UICollectionView中检索RSS源.cellForItemAtIndexPath方法设置文本并调用数据源方法以从提要中指定的链接加载图像.如果不存在,它将加载网页数据并搜索< img> 标签以获取图像.找到/加载图像后,将调用委托方法以将图像添加到单元格中.(如下)

I have an app that been happily shipping for a couple of years. It retrieves RSS Feeds in a UICollectionView. The cellForItemAtIndexPath method sets text and calls a datasource method to load an image from a link specified in the feed. If none exists it loads the web page data and searches for <img> tags to get an image. Once the image is found/loaded the delegate method is called to add the image to the cell. (below)

在iOS 8和9中运行时,一切都会很高兴,但是在iOS 10中运行时,当最初加载RSS feed但滚动时未添加任何图像,并且从cellForItemAtIndexPath中获取NULL时,可见单元会使用图像更新.

When running in iOS 8 and 9 everything is happy, but when running in iOS 10 the visible cells are updated with images when the RSS feed is initially loaded but when scrolling no images are added and I get NULL from cellForItemAtIndexPath.

当我向后滚动时会显示该图像,如果将reloadItemsAtIndexPaths添加到imageWasLoadedForStory但reloadItemsAtIndexPaths会破坏性能,则会显示该图像.

The image is displayed when I scroll back and the image is displayed if I add a reloadItemsAtIndexPaths to imageWasLoadedForStory but reloadItemsAtIndexPaths destroys performance.

- (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];
                });
        });
}

推荐答案

我鼓励所有人阅读有关Prefetching的内容-iOS 10中的新增功能.简单的解决方案是:

I encourage everyone to read up on Prefetching - new in iOS 10. The simple solution is this:

[self.customCollectionview setPrefetchingEnabled:NO];

事实证明,现在已经预取了单元格.这意味着加载的/不可见的单元格与加载的/可见的单元格之间现在有了区别.

As it turns out cells are now prefetched. This means there is now a difference between loaded/nonvisible cells and loaded/visible cells.

在iOS 10中,现在可以预加载一个单元格了,但是如果它不可见,它仍然会返回nil.因此,调用cellForItemAtIndexPath来预加载单元格.然后,如果该单元格不可见,则完全有可能图像将完成加载,并且cellForItemAtIndexPath将返回nil.这意味着将不会设置imageView.滚动时,由于该单元格已创建,因此图像不会添加到该单元格中.

In iOS 10 a cell can now be preloaded but it will still return nil if it's not visible. So cellForItemAtIndexPath is called to preload a cell. It is then entirely possible the image will finish loading and cellForItemAtIndexPath will return nil if the cell is not visible. That means the imageView will not be set. When scrolling the image will not be added to the cell since the cell was already created.

在UITableView或UICollectionView

这篇关于UIcollectionView cellForItemAtIndexPath仅在iOS 10中返回Null.在iOS 9和iOS 8中正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆