UICollectionView 的单元格消失 [英] UICollectionView's cell disappearing
问题描述
发生了什么
目前我有一个在 UITableView
中使用两个 UICollectionViews
的应用程序.通过这种方式,我创建了一个看起来像 Pulse News 的应用程序.我的问题是有时第 6 行和第 11 行完全消失,在它应该是单元格的地方留下一个空白区域.我实际上并不介意,如果所有细胞都是这样(这样我就可以假设我没有正确地做事),但问题是,只是发生在那些特定的细胞上.
Currently I have an application that uses two UICollectionViews
inside a UITableView
. This way I create a Pulse News look like application. My problem with this is that sometimes the 6th and 11th row disappears completely, leaving a blank space where it should be the cell. I wouldn't actually mind, if all the cells were like this (and this way I could assume that I wasn't doing things correctly), but the thing is, is just happening with those specific ones.
我的理论
第 6 行和第 11 行是我开始滚动时出现的行,因此默认情况下我可以看到 5 个单元格,当我进行第一次水平滚动时,第 6 行出现(有时会出现空白).
The 6th and 11th rows are the ones that appears when I start scrolling, so by default I am able to see 5 cells, and when I do the first horizontal scrolling the 6th comes up (blank space sometimes).
我有什么
我现在唯一做的就是:
[self.collectionView registerNib:[UINib nibWithNibName:CELL_NIB_NAME bundle:nil] forCellWithReuseIdentifier:CELL_IDENTIFIER];
在 viewDidLoad
上.关于单元格的创建:
On the viewDidLoad
. And on the creation of the cell:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
NSMutableDictionary *dictionary = [self.DataSource objectAtIndex:[indexPath row]];
[cell buildViewWithDictionary:dictionary withReferenceParent:self.referenceViewController];
return cell;
}
所以,我的意思是,这里没有什么花哨的东西.我虽然数据源(一个虚拟的 JSON 文件)有问题,但有时它可以正常工作并且单元格显示,所以我猜从那部分是可以的.
So on my understating nothing fancy going on here. I though there was something wrong on the data source (a dummy JSON file), but sometimes it works ok and the cell shows, so I guess from that part is ok.
所以我的问题":有谁知道发生了什么?我真的不想说这是 iOS 的错误,但我想不出别的了.
So my "question": Does anyone knows what's going on? I don't really like to say that it's a bug from iOS, but I can't think of anything else.
编辑 1.0
令人惊奇的是这个方法
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
从 indexPath
[0,4] 到 [0,6] 而不计算 [0,5].我第一次真正在 iOS 中看到这种情况发生.
Is going from indexPath
[0,4] to [0,6] without calculating the [0,5]. First time I actually see this happening in iOS.
编辑 2.0
我已经改变了创建单元格的方式,而不是出列,我使用的是旧方式:
I have switched the way I am creating the cells, and instead of dequeuing I am using the old way:
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:CELL_NIB_NAME owner:self options:nil];
MyCell *cell = (MyCell *)[nib objectAtIndex:0];
仍然是同样的悲伤结果.
Still the same sad result.
推荐答案
那么,什么有效?
1) 子类 UICollectionViewFlowLayout
.
2) 将我的 UICollectionView
的 flowLayout 设置为我的新子类.
2) Set the flowLayout of my UICollectionView
to my new subclass.
3) 在 UICollectionViewFlowLayout
子类的 init 方法上,设置你想要的方向:
3) On the init method of the UICollectionViewFlowLayout
subclass, set the orientation you want:
self.scrollDirection = UICollectionViewScrollDirectionHorizontal;
就我而言,它是水平的.
In my case it is Horizontal.
4) 重要部分:
-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
return YES;
}
此时,我应该稍微推理一下,但老实说我没有任何线索.
At this moment, I should theorise a bit, but honestly I don't have a clue.
这篇关于UICollectionView 的单元格消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!