CodeChanger
CodeChanger

Reputation: 8381

Cell disappearing after scrolling 2 - 3 times in UICollectionView

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: enter image description here

After Scroll: enter image description here

Upvotes: 1

Views: 2698

Answers (1)

CodeChanger
CodeChanger

Reputation: 8381

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.

Upvotes: 6

Related Questions