重新加载 UICollectionView 导致闪烁 [英] Reloading UICollectionView causing flickering

查看:111
本文介绍了重新加载 UICollectionView 导致闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 CollectionView 进行了子类化,并使用它来显示不同类别的图像集合.例如:最受欢迎、观看次数最多.我通过 TabbarController 访问每个类别/页面.

I have subclassed a CollectionView and using it using to display a collection of images in different categories. For example: Most Popular, Most Viewed. I access each category/page through a TabbarController.

我的课程是:

主类 - MediaList - 用于根据请求的媒体类型在集合视图中获取和显示媒体的基类.

Main class - MediaList - Base class for fetching and displaying media in a collection view depending on what type of media is requested.

SubClasses - MostPopular 和 MostViewed 都是继承自主类的轻量级类.

SubClasses - MostPopular and MostViewed are both very light classes that inherit from the main class.

问题是,当我从 MostPopular 移动到 MostViewed 时,collection view 最初显示最后一类图片,然后刷新到新的...结果你看到了一个闪烁的效果.

The problem is that when I move from MostPopular to MostViewed, the collection view initially displays the last category of pictures and then refreshes to the new...As a result you see a flicking effect.

来自 MediaList 的示例代码

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.mediaCollection.dataSource = self;
    self.mediaCollection.delegate = self;
    [self getMedia];
}

(void) getMedia 
{
    NSLog(@"/////////////////////////   GETTING MEDIA  /////////////////////////");

    XMLGenerator * sharedXmlGen = [XMLGenerator sharedXmlGenerator];
    GlobalVars * sharedGlobalVars = [GlobalVars sharedGlobalVars];
    sharedXmlGen.assetFlag = _contentFilter;
    NSLog(@"The filter is: %@", _contentFilter);
    [sharedXmlGen getPhotoVideoWithCallBackFunction:^{
    [_mediaCollection performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
}];
}

来自子类(最流行)的示例代码:

- (void)viewDidLoad
{
    [super setContentFilter:FILTER_POPULAR];  //Set the filter for the type of content
    [super viewDidLoad];
// Do any additional setup after loading the view.
}

两点说明:

  1. 我应该补充一点,我正在尝试重用尽可能多的代码,而不必在故事板或控制器上创建新视图.

  1. I should add that I am trying to re-use as much code as possible without having to create new views on the storyboard or controllers.

因为返回的媒体非常动态并且可以定期更新,所以我不得不在 viewDidAppear 上重新加载我的数据(使用 getMedia).现在,这意味着在加载新视图时,对特定页面的每次选择都会导致闪烁...

Because the media returned is very dynamic and can be updated regularly, I have had to reload my data (using getMedia) on viewDidAppear. This now means each selection of a particular page will result in the flickering when the new view is loaded...

更新- 经过进一步调查,这看起来与 uicollectionview 的子类化无关.问题只是将数据重新加载到 uicollectionview 中.可以肯定的是,我只是单击了两个子类视图之一.我还在 viewdidAppear 方法中添加了一个 reload 方法,只是为了让问题变得更糟.更容易看出问题是集合视图中的最后一组图像在新图像刷新之前最初显示了最后一类图片.然后一旦集合刷新,我就会闪烁.

Updates - After further investigation it looks like this has nothing to do with subclassing the uicollectionview. The issue instead is just reloading data into the uicollectionview. To be sure, I just clicked on one of the two subclassed views. I also added a reload method to the viewdidAppear method just to see the issue get even worse. It's also much easier to see that the issue is that last set of images in the collection view initially displays the last category of pictures before the new one refreshes. Then once the collection is refreshed I get the flicker.

*10 月 4 日更新 *开始认为我最好的解决方案是显示一个等待指示器并在加载时阻止页面.我确实尝试将 uicollectionview 设置为 nil,并相信这也能解决问题,但不确定是否可以直接重置.

*Updates Oct 4 * Starting to think my best solution is to display a waiting indicator and block the page while loading. I did try setting the uicollectionview to nil, and believe that will solve the problem as well, but not sure it will be straightforward to reset.

推荐答案

虽然在上面的代码中可能不会立即显现,但根本原因实际上是 getMedia 调用.所有子类化等都可以正常工作.

Although it may not be immediately apparent in the code above the root cause was actually the getMedia call. All of the subclassing etc. works perfectly fine.

特别是在 getMedia 调用中使用了 reload 方法并导致了问题.

Specifically having the reload method in the getMedia call and was creating the issue.

[_mediaCollection performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];

这篇关于重新加载 UICollectionView 导致闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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