Wayne Filkins Wayne Filkins - 1 year ago 61
Swift Question

Is there a way to directly access UICollectionView elements without reloading?

I have another question open where I'm trying to figure out how to reload the collectionView without auto-scrolling. I was also realizing there are a lot of other situations where I will need to change things in the collection view. Also I have some items that I will want to change the .alpha on and change the text of. Is there a way to do all of this in Swift? For example (to be specific) if I have a collection view with a view in each cell and that view has a textField in it, can I change the alpha and text, (change alpha with animation even) without reloading entire table?

Answer Source

Look at the documentation for UICollectionView. There are several "reload" methods:

  1. reloadData()
  2. reloadSections(_:)
  3. reloadItems(at:)

If you just want to reload a single item, update your data source's data and then call reloadItems(at:) passing in the index path for the item.

Another option, if a cell is currently visible, is to use the cellForItem(at:) method to get a reference to an existing cell. Then you can directly access UI components of the cell as needed. You should also update your data model as needed so if the user scrolls and comes back, the cell will be rendered properly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download