UICollectionView elements disappear when scrolling with my own subclass UICollectionViewFlowLayout

Context: I use UICollectionView for taking UICollectionView . Each image is a cell with one UIImage . Images can have different sizes, and I want them to fill the entire screen. So I wrote a class that defines the frame of each UICollectionCell and allows the subclass of UICollectionViewFlowLayout to set this class to the correct frame for each element.

My implementation of UICollectionViewFlowLayoutClass:

 override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? { let attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes] for attributes in attributesToReturn ?? [] { if attributes.representedElementCategory == .Cell { let frame = self.layoutAttributesForItemAtIndexPath(attributes.indexPath).frame attributes.size = frame.size attributes.frame = frame } } return attributesToReturn } override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! { let curAttributes = super.layoutAttributesForItemAtIndexPath(indexPath) let frame = mazeManager.sizeForItemAtIndex(indexPath, collectionView: collectionView!) curAttributes.size = frame.size curAttributes.frame = frame return curAttributes } 

So, the frame asks my MazeManager to return the frame. The returned frames seem correct, and they all fit into the UICollectionView.

When I open my application, everything looks good, even when I scroll. But when I move to a certain position (this position seems random, because it depends on the images with which I am testing, but with the same set of images, the positions are the same), the cells disappear from my view. When I go back, they come back.

I checked if the cells were hidden, but they were never.

In some other threads with similar problems, the answer is to implement collectionViewContentSize , so I did:

 override func collectionViewContentSize() -> CGSize { let size = mazeManager.collectionViewContentSize return size.height < collectionView!.frame.size.height ? collectionView!.frame.size : size } 

The number of elements is not static, and it grows when the end of the view is reached. So what happens here: The manager determines Origin.y + Size.Height of the last element (+ 10 points), the width is the width of the UICollectionView.

However, all cell frames are within the size returned by this method. But still, some cell disappears or does not appear at all.

When I scroll further through the UICollectionView, I see other cells that are in the right place. In my opinion, there are gaps, but the flow continues. (When a space appears, there are no calls for elements missing idexpaths in collectionViewDelegate. For example, CollectionView requests elements: 1,2,3,4,5,6, 12,13,14).

The console does not print anything about incorrect positioning, I checked and everything is in ContentSize. Therefore, I have almost no options. Can someone explain what is happening in my case?

Thanks.

Edit:

While I was looking for a solution, I already found the post mentioned (the UICollectionView cell disappeared ), and I already took 3 of 4 steps.

 override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool { return true } 

And I just added the scroll direction in the initializer:

 override init(){ super.init() scrollDirection = .Vertical } required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) self.scrollDirection = .Vertical } 

Unfortunately, this does not fix my problem, so for me it is not like.

+7
ios objective-c iphone swift
source share
1 answer

I don't know why, but it works when I add the following line to my init methods:

  self.itemSize = CGSize(width: 165,height: 165) 

165 is the average height for my cells (I need to make this less static). The size shown here seems to be ignored because the sizes that I see on my screen are calculated in layoutAttributesForItemAtIndexPath .

Thus, without this property, the task looks strange, but I still don't know the reason, but I'm glad that it works. (If anyone knows the reason, I would love to hear it)

+1
source share

All Articles