Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - applyLayoutAttributes does not get called for some of the cells

I am creating a view that is like the springboard screen in iOS. (This is a follow-up-question to this question). My implementation is a collection view, that has a custom layout using UICollectionViewLayout. This layout is build according to the code that was published in this answer. Since I want to have my icons to shake and have a little delete button in the upper-left corner, I have subclassed UICollectionViewLayoutAttributes, with a new attribute of deleteButtonHidden. All of this works well together. But I have a problem when I populate the collection view with more icons to fit into one page. When I enter edit-mode, and scroll back and forth, I get that some icons are not shaking anymore. I tried to debug it, and I realised that the method applyLayoutAttributes does not get called for some of my cells. I also tried to call invalidateLayout inside the method scrollViewDidScroll, but that didn't help.

Does anyone have an idea why some cells don't get their attributes applied?

UPDATE: I just found out that Apple has a patent for jiggling icons. So implementing this design is not going to be approved. Therefore, this question is not relevant anymore.

like image 547
bobsacameno Avatar asked Jun 02 '14 13:06

bobsacameno


2 Answers

Apple specified that you should implement isEqual: method when subclassing UICollectionViewLayoutAttributes.

If you subclass and implement any custom layout attributes, you must also override the inherited isEqual: method to compare the values of your properties. In iOS 7 and later, the collection view does not apply layout attributes if those attributes have not changed. It determines whether the attributes have changed by comparing the old and new attribute objects using the isEqual: method. Because the default implementation of this method checks only the existing properties of this class, you must implement your own version of the method to compare any additional properties. If your custom properties are all equal, call super and return the resulting value at the end of your implementation.

I hope it will help to you.

like image 120
kelin Avatar answered Oct 13 '22 01:10

kelin


The issue is that if you subclass UICollectionViewLayoutAttributes you need to implement copyWithZone: in your subclass because the superclass implements it.

So just add your copyWithZone: implementation and it should work.

like image 27
Ivan Andriollo Avatar answered Oct 13 '22 00:10

Ivan Andriollo