iOS Tutorial: Collection View and Diffable Data Source |

In this iOS tutorial, you’ll learn how to implement a collection view with UICollectionViewDiffableDataSource and NSDiffableDataSourceSnapshot.

This is a companion discussion topic for the original entry at

Hi! I was wondering it it’s possible to always have a section header visible in the collection view using the diffable data source, even if there are no items in that section?

Absolutely! Within the context of the sample project-- if you add a Section without any videos, the section header is still visible and can be anything you want. Otherwise, you could add an Item enum and have two options: .video, and .text. Each of these could inform the data source which type of cell to display, and visually has the same effect as the supplementary views do. I hope this answers your question! :]

Having a problem using Interface Builder to create the Section Header Reusable view rather than using the manually coded one that comes with the lesson.

After checking the “Accessories - Section Header” in the Collection View IB, I layout the Header in IB.

Then, I create a simple supplementary header “SectionHeaderReusableViewIB” and use it as the class for the header Interface Builder with a subclassed UICollectionReusableView to match it instead of using the "SectionHeaderReusableView”.

I then change the code from using “SectionHeaderReusableView” to “SectionHeaderReusableViewIB”.

Unfortunately, when I run the app and reach the following logic:

view?.titleLabel.text = section.title

The titleLabel is nil and the app bombs.

A reusable view is returned fine, but the titleLabel is nil.

When I change it to:

view?.titleLabel?.text = section.title

The app runs fine, but the title does not display.

Does that mean using the Diffable Data method way, Reusable Views must be coded manually rather than using Interface Builder?

Hey Randy!

You shouldn’t need to create reusable views using code (though I typically recommend using code to avoid massive storyboards and their associated problems!).

I would check and make sure your label outlet is connected correctly. From there, you could set a breakpoint in the awakeFromNib() function inside of your reusable view, and check the outlets using Xcode’s debugger.

What about using Diffable with CoreData and FetchedResultsController?

It was indeed really helpful. I tried using this. I have one problem, How can i implement Tableview/CollectionView with data from API using pagination. Now there is no cellforRowAtIndexPath , where do we put pagination logic

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

Hi! Please refer to our tutorial on pagination, which should integrate well with diffable data sources:

When you implement your collection view and the NSFetchedResultsControllerDelegate, you can apply a new snapshot inside of the controllerDidChangeContent function to update your data.

At the beginning of the passage you said copy the code and paste it under videoList in videoViewController.swfit, but I can’t find any related in the swift file. May I know it there anything that I misunderstood?

I have a question about the behavior of the api of NSDiffableDataSourceSnapshot.
can you help me?

Thanks for producing such high quality tutorial. It is easy to be followed and be understand. However, I do have some doubt on your Video class. I was wondering, is it ever correct, to only consider id member (instead of all members) in your hash and equals function?

Isn’t that create chance to produce a equals function which returns true, even for 2 Objects with different content?

I also ask my doubt in stack overflow -

Thank you.

I created a collection view with a diffable data source after following this tutorial. It all worked great apart from the memory issues it created. I needed to add [weak self] to the datasource closure when creating the UICollectionViewCell.


dataSource = UICollectionViewDiffableDataSource<CollectionName, ItemName>(collectionView: collectionViewName) { [weak self] (collectionView: UICollectionView, indexPath: IndexPath, item: ItemName) -> UICollectionViewCell? in

Was this the correct way to avoid the memory issues? The view was being retained when unwinding a segue, instead of destroyed.



I came across a similar problem recently and you’re right about the strong reference cycle here. I downloaded the materials for this tutorial and the problem in the final version of the project is inside the makeDataSource() method and specifically at the line the supplementaryViewProvider is assigned. dataSource.supplementaryViewProvider = { collectionView, kind, indexPath in.

Inside that closure the self.dataSource is captured in order to get the current section and this causes a strong reference cycle. You need to add [weak self] at the line above like this dataSource.supplementaryViewProvider = { [weak self] collectionView, kind, indexPath in

As a rule of thumb, if you capture self inside the cellProvider or the supplementaryViewProvider closure, you need to add [weak self] otherwise you’ll end up having a memory leak due to a strong reference cycle.

1 Like

@josteberg In the tutorial we use only Video data type. Any tips how to handle variable data types? Also, when there is no data for a any given section, I’d like to be able not to display it at all. How would you recommend handling sections when the data varies per section? For example first section uses String for cells, second section(optional) uses XYZModel for cells, third section uses another data type.


Given that adopting iOS 13 as the base SDK is usually a privilege, could you suggest some method to actually implement a given collectionview for something like iOS 11/12 which still using DiffableDataSource for iOS 13 and higher?


It is awesome tutorial, but how can I write unit tests for this implementation.