Skip to content

Prevent multiple layout attrs for the same object#166

Merged
chiahsien merged 2 commits intochiahsien:developfrom
rafaelcr:patch-1
May 23, 2017
Merged

Prevent multiple layout attrs for the same object#166
chiahsien merged 2 commits intochiahsien:developfrom
rafaelcr:patch-1

Conversation

@rafaelcr
Copy link
Copy Markdown
Contributor

@rafaelcr rafaelcr commented Mar 8, 2017

I've been getting a crash recently when trying to add headers to sections using your layout:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'layout attributes for supplementary item at index path (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}) changed from <UICollectionViewLayoutAttributes: 0x6100003e1f00> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (CHTCollectionElementKindSectionHeader); frame = (0 490; 768 20);  to <UICollectionViewLayoutAttributes: 0x6080003e1200> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (CHTCollectionElementKindSectionHeader); frame = (0 530; 768 20);  without invalidating the layout'

What I noticed is that multiple attributes were being generated for the same index path in layoutAttributesForElementsInRect so the UICollectionView got confused towards which one to use. This fix prevents that from happening.

rafaelcr added 2 commits March 8, 2017 16:56
I've been getting a crash recently when trying to add headers to sections using your layout:

```
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'layout attributes for supplementary item at index path (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}) changed from <UICollectionViewLayoutAttributes: 0x6100003e1f00> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (CHTCollectionElementKindSectionHeader); frame = (0 490; 768 20);  to <UICollectionViewLayoutAttributes: 0x6080003e1200> index path: (<NSIndexPath: 0xc000000000000116> {length = 2, path = 1 - 0}); element kind: (CHTCollectionElementKindSectionHeader); frame = (0 530; 768 20);  without invalidating the layout'
```

What I noticed is that multiple attributes were being generated for the same index path in `layoutAttributesForElementsInRect` so the `UICollectionView` got confused towards which one to use. This fix prevents that from happening.
NSMutableArray *attrs = [NSMutableArray array];
NSMutableDictionary *cellAttrDict = [NSMutableDictionary dictionary];
NSMutableDictionary *supplAttrDict = [NSMutableDictionary dictionary];
NSMutableDictionary *decorAttrDict = [NSMutableDictionary dictionary];
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need 3 dictionaries? Or what we need is one dictionary and use attr.indexPath as the key?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is indexPaths can be the same and collide between each of those three object categories. I actually tried that approach on the first commit but it didn't work because of that. Items were overwritten by decorators or supplemental views and viceversa.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️This will cause a new BUG

@chiahsien chiahsien merged commit a7e5cff into chiahsien:develop May 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants