Wayne Filkins Wayne Filkins - 10 months ago 47
Swift Question

Can UICollectionViewUpdateItem be used to update a single label?

I'm trying to update a single label in a collection view cell. I know how to update the entire collection view and how to update a single cell, but I want to update only a label because if the whole cell updates it will change other stuff which I don't want to change yet. In the docs I found something called UICollectionViewUpdateItem but cannot figure out how to use it. Is this capable of updating a single label or if not is there another way?

Answer Source

If you know the index path of the cell you want to update, you can get the cell using cellForItem(at: IndexPath). From there you can edit your cell's label.

However, I suggest you don't change your underlying data so that it's in a state that is unsuitable for presentation in the UI. The collection view can decide to create cells at times that are unpredictable to you.

UICollectionViewUpdateItem is used to indicate e.g. from where a cell moved, and where it moved to, or that a cell at a certain position was deleted, updated, etc. The docs state:

You do not create instances of this class directly.

So it means you should only create it indirectly, for example through reloadItems(at: [IndexPath]). It will reload your entire cell and should not be used to update a single detail of it.