Modern collection views

Tram Ho

  • During the two-year period from iOS13-iOS14 we have seen a number of changes coming from Apple regarding the UICollectionView and the type associated with it. Not only are the new API introduced, but some of the concept and concepts that were used to build collectionview have been changed and updated according to new programming models. In this article we will cumgf explore the new models to further understand how these collection work.

1: Diffable data sources:

  • One of the problems we have often encountered when working with collection view on systems pre- iOS13 comes from the real-world scenarios when all updates need to be manually deployed by developer (like usage performBatchUpdates ). The manual implementation above often causes app crash when the update do not end up concurrently with the data model being used.
  • Using UICollectionViewDiffableDataSource means that we will let that class calculate the change in the collection view ‘s state and automatically update the necessary changes to the data display.
  • We take the example of ProductListViewController display the product . To viewcontroller use DiffableDataSource then first we must initialize a cellProvider closure to transfer indexPath for UICollectionViewCell as follows:

  • Using Swift strong type way on ensuring type safe for the model data as well as enabling us can custom the type Hashable definitions for Section instead of always using Int :

  • What we need to do now is we need to assign data to the collection view like we did before:

  • Once the data model have been update we need to add a describe of the current view states to the dataSource so that the cell can automatically track and update as needed.
  • We will use the snapshop concept for the section defined and update each section from the data model . Finally we take the snapshot dataSource by updating the collection view after comparing the previous change:

  • It should be noted here is that we are moving the model Product directly dataSource by confirm Hashable . The above approach is problematic if we have a data model that cannot confirm the above protocol so we can pass some formatting to the dataSource and then proceed to the complete model in the cellProvider closure.

2: Cell registrations:

  • Cell registrations is a new concept in iOS14 allows us to identify the usage of the UICollectionViewCell subclass as well as how we customize the collectionview cell with complex objects. We won’t need to remember having to declare cell types themselves for the reuse identifier job and cells won’t need type casting .
  • We will use the new API to implement the registration and configuration for the collectionview cell with the ProductListViewController as follows:

  • We can review the makeDataSource method and change cellProvider as follows:

  • We have just significantly improved the previous code with cellProvider closure that directly takes over cell registration . We’ll need to add an extension here:

  • With the above extension we have reduced the number of lines of code above as follows:

3: Compositional layouts:

  • Before iOS13 we had two options to customize the layout for UICollectionView . The first way is to use UICollectionViewFlowLayout and we’ll go through the first steps of this selection:
  • We need clear definitions for compositional layout including: items, groups, sections. Item for the layout of cell , groups for layering cells together and sectionsexes that include sections for collectionview .
  • We want the layout for the product list views with featured and onSale sections using 2 column grid while the sections are using full-width :

  • The strong point of compositional layout is that we can use multiple layouts within a viewcontroller as well as we can describe desired layout using fractional values :

  • To optimize the above code we will use NSCollectionLayoutSection to get the section index in the Int format:

  • The last thing we need to do is inject the above code every time the collectionView is initialized:

4: List views and content configurations:

  • In iOS14 we can completely build table view using UICollectionView . To render sections we can simply use the previous list definitions instead of creating our own:

  • The above code is quite optimized, we do not need to write custom layout code anymore, just reuse insetGroup to get the desired layout :

  • We can also create and use the type type`UICollectionViewListCell as a copy from UITableViewCell so we can render text , image as well as accesories as indicator . makeCellRegistration method can be customized for us to use as follows:

Share the news now

Source : Viblo