滚动后,UICollectionView的单元格不会始终刷新 [英] Cell of UICollectionView not always refreshed after a scroll

查看:162
本文介绍了滚动后,UICollectionView的单元格不会始终刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Storyboard(iPad)中定义了一个包含 UICollectionView 的视图,在视图的底部还有一个 UIToolbar
UICollectionView 中我添加了 UICollectionViewCell (由 iPadCellCollectionViewCell实现 class)包含另一个核心图表图表( CPTGraphHostingView 类)。

I have defined in my Storyboard (iPad) a view that contains a UICollectionView, at the bottom of the view there's also a UIToolbar. In the UICollectionView I have added a UICollectionViewCell (implemented by an iPadCellCollectionViewCell class) that contains a another view that is a Core-Plot Graph (a CPTGraphHostingView class).

我有一个名为X的类,它实现 UICollectionViewDelegate UICollectionViewDataSource

I have a class called X that implements the UICollectionViewDelegate and UICollectionViewDataSource.

在X类中,我为我的视图的每个单元格(在 ViewDidLoad 中)构建Core-Plot Graph并且我有以下代码将图表链接到 UICollectionViewCell的单元格

In the class X, I build for each cell of my view (in ViewDidLoad) the Core-Plot Graph and I have the following code to link the graph to the cell of the UICollectionViewCell

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellId = @"ChartCollectionId"; 

iPadCellCollectionViewCell* cell = [self.theCollectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];

MyChart* chart = [_charts objectAtIndex:indexPath.row];

cell.hostingView.hostedGraph = chart.barChart;

return cell;

}

它工作正常,全部我的图表正确显示在屏幕上。当用户滚动视图时,会出现问题,然后一个或多个单元格变为空白。我真的不明白为什么。

It's working fine, all my Graphs are correctly displayed on the screen. The issue happens when the user scroll the view, then one or several cells become "blank". I really don't understand why.

我找到了一个解决方法,而不是在<中添加 CPTGraphHostingView 接口生成器中的code> UICollectionViewCell ,我自己构建它,之前的代码变为:

I have found a workaround, instead of adding the CPTGraphHostingView in the UICollectionViewCell in the Interface Builder, I build it myself and the previous code becomes:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString* cellId = @"ChartCollectionId"; 

iPadCellCollectionViewCell* cell = [self.theCollectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];


[cell.contentView addSubview:[_hostingViews objectAtIndex:indexPath.row]];

return cell;

}

使用此代码,一切都是工作正常。当我在IB的 UICollectionViewCell 中添加 CPTGraphHostingView 时,有什么解释为什么它不起作用?

With this code everything is working fine. Is there any explanations why it's not working when I'm adding the CPTGraphHostingView in the UICollectionViewCell with the IB?

每次调用方法时调用addSubview是一个问题吗?没有内存泄漏?

Is it an issue to call "addSubview" each time the method is called? No memory leaks?

推荐答案

这在使用sectionInsets时似乎是UICollectionView中的一个错误。
单元格不会被隐藏,有时在快速滚动时它们被错放了。

This seems to be a bug in UICollectionView when using sectionInsets. The cells don't get invisible, they are misplaced out of bounds sometimes on fast scrolling.

所以如果你通常每行有3个项目,那么你将在一行中获得第四项,但在下一行的第一个位置没有项目。

So if you normally would have 3 items per row, you will get a forth item in one row, but no item at the first spot of the next row.

如果将UICollectionView设置为不剪切到边界,并将其移动在您的UI之上,
您可能会看到这个未对齐的单元格。

If you set your UICollectionView to not clip to bounds, and move it on top of your UI, you might see this misaligned cell.

我的解决方案到目前为止还没有使用sectionInsets,并使用contentInset包围它部分标题中的空白空间。

My solution was so far to not use sectionInsets, and surround it by using contentInset and some empty space in the section headers.

我还在Twitter上找到了其他一些同样问题的人。
还有关于它的错误报告:12353513

I also found some other guys at Twitter with the same problem. There's also a bug report about it: 12353513

这篇关于滚动后,UICollectionView的单元格不会始终刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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