UICollectionView Custom Layout Tutorial: Pinterest

Hello, using the example would be possible to make it appear as the image?

did ya got any answer on this? i want to do someting similar

I have downloaded final project and tried to run in Xcode 8.3.2. Initially it has asked me to convert it in Swift 3.0 so I have converted it and resolved few syntax errors. Now project is running but not loading the collectionview. The cellForItemAtIndexPath method is not getting called. Any one please suggest.

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) β†’ UICollectionViewCell

If you control the database, you can keep in the database the height of the image ( I would also keep the width) so that when you download the annotation you get also the height that you need to build the frame of the image.

Has anyone got it to implement with pagination? This tutorial is nice, but it fails to convey things such as downloading objects from the internet and inserting more cells into the layout (in the bottom for example). Has anyone achieved this? If so, please enlighten me.

1 Like

For everyone that’s having trouble updating the UICollectionView with new cells, add this to your Layout subclass to empty the cache when the layout is reloaded:

override func invalidateLayout() {
    super.invalidateLayout()
    cache.removeAll() // Empty the cache from the tutorial
}

Isn’t that inefficient? Because now It will have to make a brand new layout for what was already there and the new cells. Is there no way to reuse whats already there?

@anon89822885 Do you have any feedback regarding all of this? Thank you - much appreciated! :]

I have added this method inside my custom layout class but with no success.
My app is crashing at
guard cache.isEmpty == true , let collectionView == collectionView else {
return
}

Any help would be much appreciated.

@anon89822885 Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi for me this is working :-

step 1 :-
Existing code

fileprivate var cache = UICollectionViewLayoutAttributes

Remove fileprivate
var cache = UICollectionViewLayoutAttributes

step 2 :-
Write down below code when you want to reload collectionview -
let layout = self.collectionView?.collectionViewLayout as? PinterestLayout
layout?.cache.removeAll()
self.collectionView?.reloadData()
layout?.prepare()
step 3 :-
Enjoy :smile:

This tutorial is more than six months old so questions are no longer supported at the moment for it. Thank you!