在 UICollectionView 中滚动 2 - 3 次后单元格消失 [英] Cell disappearing after scrolling 2 - 3 times in UICollectionView

查看:78
本文介绍了在 UICollectionView 中滚动 2 - 3 次后单元格消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

它奇怪地隐藏了我的手机或移除了我的手机,我不知道,但我找不到任何解决方案.

我已经从 SO 中尝试过这些解决方案:

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

第二个:尝试了以下方法来重新分配和重新计算我的单元格大小:

-(BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{返回是;}

我关注的链接:

Que1:

滚动后:

解决方案

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

因为我在这个问题上被困了过去 2 天.

当我使用 CollectionViewController 并将 Controller 视图添加到我当前使用的控制器中时,它就会消失我的集合视图单元格.

<块引用>

原因:由于 ARC 释放了 delegate &datasource 连接和该集合视图无法获取单元格,而我从上到下滚动.

解决方案:

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

1st : 在我当前的 VC 中而不是在 Service & 中创建 CollectionViewControllerJSON 解析器类由于 delegate &数据源连接丢失.

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

UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@Main";捆绑:[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];

<块引用>

但根据我的要求,我在很多地方都需要这个集合视图所以我需要概括这个集合视图.

所以我选择了第二种方式,即 ContainerViewadd child controller 并将这个 CollectionViewController 与容器视图连接起来,这样我就可以使用它了尽可能多的地方需要这个列表.

奇怪但真实.

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

谢谢&快乐编码.

I'm facing a strange issue while scrolling my UICollectionView. It's showing a blank view without any cells in it.

It strangely hides my cell or removes my cell, I don't know, but I'm unable to find any solution.

I have tried these solutions from SO:

1st : Tried to create custom class of UICollectionViewFlowLayout and do as directed with all different methods but unable to solve my issue.

2nd : Tried below method to reattribute and recalculate my cell size :

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

Links I followed :

Que1: UICollectionView's cell disappearing

Que2: UICollectionView items disappear when scrolling with my own UICollectionViewFlowLayout subclass

Please guide me whats wrong with simple collectionview with custom cell.

Reference Image :

Before Scroll:

After Scroll:

解决方案

Here I am answering my own question.

As I am stuck for the last 2 days on this issue.

As I am using CollectionViewController and add that Controller view into my current using controller so it's disappearing my collection view cell.

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.

Solution:

I solve this problem in 2 ways.

1st : Create that CollectionViewController in my current VC not in Service & JSON Parser class due to delegate & datasource connection loss.

The most important part in the below code is addChildViewController by this it will not releasing delegate connection and works as expected.

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.

So I choose the 2nd way which is ContainerView with add child controller and I connect this CollectionViewController with container view so I can use it as many places where I need this list.

Strange but true.

Hope this will help someone if facing the same issue.

Thanks & Happy Coding.

这篇关于在 UICollectionView 中滚动 2 - 3 次后单元格消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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