在 UICollectionView 中滚动 2-3 次后单元格消失

Cell disappearing after scrolling 2 - 3 times in UICollectionView

我在滚动 UICollectionView 时遇到了一个奇怪的问题。它显示的是空白视图,其中没有任何单元格。

它奇怪地隐藏了我的单元格或删除了我的单元格,我不知道,但我找不到任何解决方案。

我已经尝试过来自 SO 的这些解决方案:

1st : 尝试创建 UICollectionViewFlowLayout 的自定义 class 并按照指示使用所有不同的方法进行操作,但无法解决我的问题。

第 2 次: 尝试了以下方法来重新归因并重新计算我的单元格大小:

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds
{
   return YES;
}

我关注的链接:

问题 1:UICollectionView's cell disappearing

队列 2:

请指导我使用自定义单元格的简单集合视图有什么问题。

参考图片:

滚动前:

滚动后:

我在这里回答我自己的问题。

因为我在过去 2 天里一直在这个问题上卡住了。

因为我正在使用 CollectionViewController 并将 Controller 视图添加到我当前使用的控制器中,所以它正在消失我的 collection 视图单元格。

Reason: due to ARC releasing that delegate & datasource connection and that collection view can't able to get cell while I am scrolling from top to bottom.

解法:

我用两种方法解决了这个问题。

1st : 在我当前的 VC 中创建 CollectionViewController 而不是在服务中 & JSON 解析器 class 由于 delegate & datasource 连接丢失。

下面代码中最重要的部分是addChildViewController它不会释放委托连接并按预期工作。

UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
CardListViewController *objCardListViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"CardListViewController"];
objCardListViewController.view.frame = CGRectMake(0, 0,self.cardMainView.frame.size.width, self.cardMainView.frame.size.height);
[self.cardMainView addSubview:objCardListViewController.view];
[self addChildViewController:objCardListViewController];

but as per my requirement I needed this collection view at many places so I need to generalize this collection view.

所以我选择第二种方式,即 ContainerView 添加 child 控制器 ,然后我将此 CollectionViewController 与容器视图连接,所以我可以在我需要此列表的任何地方使用它。

奇怪但真实。

希望这对遇到同样问题的人有所帮助。

谢谢,编码愉快。