njdehoog / NHBalancedFlowLayout

UICollectionViewLayout subclass for displaying items of different sizes in a grid without wasting any visual space. Inspired by: http://www.crispymtn.com/stories/the-algorithm-for-a-perfectly-balanced-photo-gallery
MIT License
1.34k stars 106 forks source link

Adding new items to the collection rearranges old cells #21

Open limenutt opened 10 years ago

limenutt commented 10 years ago

Hi there. In my Photozeen iOS app I'm loading more images to the photo feed as user scrolls it down. Every time I have new portion of images from server I add them to the collection by calling insertItemsAtIndexPaths. I see that items are getting rearranged every time. What can I do to make previously loaded items stay still except adding new section for every new portion of images?

njdehoog commented 10 years ago

Are you adding every new batch of items to a different section, or are all the images in the same section. The layout is calculated per section, so adding a new section should allow the previous section to remain unchanged. Although I'm not sure if it would redo the calculation for the previous sections as well.

limenutt commented 10 years ago

Yes, currently I'm using only one section and was looking for workaround without adding new section for every new batch. But looks like it is the only solution, am I right?

njdehoog commented 10 years ago

That's right. The implementation of the layout algorithm is such that it tries to determine the best possible layout based on every image in a section, so every time you add a new image it is likely to affect the layout.