UICollectionView Custom Layout Tutorial: Pinterest

Anyone know the reasons that we need to add UICollectionView as the first parameter in the protocol method?

I found that the parm has not be used any where and I deleted it and the app still work fine.

protocol PinterestLayoutDelegate: class {
func collectionView(_ collectionView:UICollectionView, heightForPhotoAtIndexPath indexPath:IndexPath) โ†’ CGFloat
}

This tutorial is worth GOLD. I finally understand much better how to work with UICollectionViewLayout sub class. Thanks a lot!! Also read the Apple documentation about this. Very helpful.

It is interesting. I didnโ€™t know it is so straightforward. Basically just calculate the frames of each item with a loop. I thought it is way more complex than this. When I was working with SpriteKit before this, where of course you donโ€™t have collection views I had to manually calculate the frames of each item with a loop (letโ€™s say 10 boxes of the same width and height) with i * (size + padding). But with dynamic heights I just have to increase the Y offset after every loop.

Iโ€™m doing this by overriding invalidateLayout in the custom layout class. I clear the cache there.

First of all great tutorial. Thanks for the great example. I used it to build a custom layout for my collection view. In my layout I have a list of tags that are filterable by a search bar. I was having some seemingly random crashes and fixed it by calling super.prepare() in the overridden prepare() function. It fixed my crashes. Not sure if anyone is running into anything like. Should we be calling super.prepare()?

Hi,

I am using your strategy but still getting a crash at line
โ€œguard cache.isEmpty == true , let collectionView == collectionView else {โ€

Any solution or hints would be appreciated.

Thanks

I am facing exactly the same issue. Crash occuring at line
guard cache.isEmpty == true, let collectionView = collectionView else {
return
}
I am getting data from web service and then reloading the collection view .

Can you please explain in more detail how to implement purge cache policy. Where to purge the cache. and when to invalidate the collection view layout ?

Any help would be much appreciated.

Thanks

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

1 Like

If you add a translucent Navigation bar, the collection view will be underneath (and the same with searchbar). How can I solve this Problem without hard coded Offsets?

Hi, thanks for the tutorial. can someone tell how I can add custom sections header to this please?

Thanks for the help =)

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

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