UUID over IndexPath

Rafał Prążyński
Nerd For Tech
Published in
3 min readApr 15, 2021

--

Hello there!

Today I want to suggest start using UUID to find the proper model in list instead of using IndexPath when item in UICollectionView or cell in UITableView is selected.

For the purpose of this article, I assume you’re familiar with retrieving proper model from array using IndexPath.

I’ll use project from my previous article Resolve reusable Cell problem and adjusted it a little bit to properly present the intention of this article.

So what’s going on in the individual classes:

MOCK.swift

I have declared non sorted response here that contains the name of the image to display. Response model (Pepe) is wrapped in DTO (PepeDTO) structure which contains property ID of type UUID. This ID will be used for the identify model in list which I’ll show later.

Pepe structs

CollectionView.swift

Here I’ve declared a protocol CollectionViewCellInteractionProtocol with the following method:

func selectedItem(at indexPath: IndexPath)

This method will delegate action out side of UICollectionView.
It’s a good practice not to create tight connections between objects and using protocol for the decoupling is a good way too. This approach gives us more flexibility.

Call method from CollectionViewCellInteractionProtocol in UICollectionViewDelegate method:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)

CollectionViewCellBuilder.swift

Here I’ve declared a protocol CollectionViewCellBuilderProtocol with the following method. :

func selectedModel(with id: UUID)

We need this method to inform outside of the CollectionViewCellBuilder which UUID was selected.

Also PepeBuilder (here is the article showing how it works: Resolve reusable Cell problem) contains declaration of closure selectedModel, for handling user interaction on cell and which will be called in implementation of method from protocol CollectionViewCellInteractionProtocol

How it all works:

  1. In ViewController not sorted mocked models are created (PepesModel), held at global variable and passing to CollectionViewCellBuilder also ViewController is implementing CollectionViewCellBuilderProtocol.
  2. In CollectionViewCellBuilder are sorted lists of PepeDTO by its property called name, created ViewModels of PepeCollectionViewCell, passing those ViewModels to PepeBuilder and implement closure selectedModel. In implementation selectedModel is a method is called from CollectionViewCellBuilderProtocol for passing UUID according to the selected cell.
  3. ViewController received selected UUID from CollectionViewCellBuilder and retrieves the model from not sorted list.
  4. Let’s assume user taps item at index 4, then we should see these logs:

What it give us?

In the example above we have two different lists: the first is sorted, the second isn’t. Thanks to UUID we have no need to keep sorted the second one. Also if any change of order of the presented cell occurs, we do not need to worry about index synchronization of two separated lists.

Thanks for reading, and Good luck!

Full code: https://github.com/Rafal-Prazynski/UUID_over_IndexPath

--

--