Strange endangered items on scrolls

Presets

I have a subclass of classViewFlowLayout with

- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return YES; } - (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect { NSArray *arr = [super layoutAttributesForElementsInRect:rect]; BBLog(@"ARRA:%@", arr); for (UICollectionViewLayoutAttributes *attr in arr) { if (CGAffineTransformIsIdentity(attr.transform)) { attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); } } return arr; } 

CollectionView pivots upside down with

  self.collectionView.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); 

But even if jus uses its own collectionViewFlowLayout without a subclass, git this error

Problem

I have two messages and more in the chat, but when the scroll at the bottom (top is normal) the second element disappears.

layoutAttributesForElementsInRect for a given direct return two attributes for two indexPaths 0-0 and 0-1, but the delegation method

 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 

called only for indexPath 0-0

Here images

Topscroll enter image description here

UPDATE So I found WHY this will happen - this line code

 attr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); 

Look, delete the transform

enter image description here

+8
ios uicollectionview uicollectionviewcell uicollectionviewlayout uicollectionviewdelegate
source share
2 answers

Sorry, everything, but I found the reason and worked.

This only happens on the device, not on the simulator.

Look at the three CGRects

iPhone 5s

 (CGRect) oldFrame = (origin = (x = -0.000000000000056843418860808015, y = 0), size = (width = 320.00000000000006, height = 314.00000000000006)) 

iPhone 5C

(CGRect) oldFrame = (origin = (x = 0, y = 0), size = (width = 320, height = 314))

Intel Core Simulator

 (CGRect) oldFrame = (origin = (x = 0, y = 0), size = (width = 320, height = 314)) 

For all of them, I apply rotation conversion through

 CGAffineTransformMakeRotation((CGFloat)M_PI) 

First it is on the iPhone 5S ARM Apple A7 CPU

The second is on the iPhone 5C ARM Apple A6 CPU

Thirdly, it is on a simulator on an Intel Core iX processor.

So my job is:

 - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewLayoutAttributes *retAttr = [super layoutAttributesForItemAtIndexPath:indexPath]; if (CGAffineTransformIsIdentity(retAttr.transform)) { retAttr.transform = CGAffineTransformMakeRotation((CGFloat)M_PI); } CGRect oldFrame = retAttr.frame; oldFrame.origin.x = retAttr.frame.origin.x > 0 ?: 0; retAttr.frame = oldFrame; return retAttr; } 
0
source share

I'm not quite sure, but I think that when you subclass UICollectionViewFlowLayout, you should not change the attributes directly, but to make a copy of the attributes, change it and return it.

A brief explanation of the top statement: You will have to subclass UICollectionViewFlowDelegate (the parent element of UICollectionViewDelegateFlowLayout) rather than creating your own attributes and changing them as you like, but for this you will need to implement a lot more user logic.

Also see if you get any errors or warnings in the console.

Take a look at this question: Warning: UICollectionViewFlowLayout has cached frame mismatch for index path 'abc'

I hope that I was at least a little helpful.

+1
source share

All Articles