Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent multiple layout attrs for the same object #166

Merged
merged 2 commits into from
May 23, 2017

Conversation

rafaelcr
Copy link
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
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
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.

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