本文介绍了UICollectionView的单元格消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

发生了什么

目前我的应用程序内部使用了两个 UICollectionViews a UITableView 。这样我创建了一个类似应用程序的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).

我拥有什么

我目前唯一做的事情是这个:

The only thing I am doing at the moment is this:

 [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)在<$ c的init方法上$ c> UICollectionViewFlowLayout 子类,设置你想要的方向:

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的单元格消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 09:19