Updating Cell Accessories after Configure?

I've got an asynchronous validation method that validates the underlying model of a cell when the cell is displayed on screen. When the validation completes, I modify the cell accessories to include an additional button that can be tapped for error details.

This validation is invoked within a  UICollectionView.CellRegistration.

It seems that modifying the cell-accessories after the cell has returned (via the async completion handler) does not trigger a view update of said cell.

Should it? Or is it better for me to persist this information in the model object and trigger a the snapshot update?
Answered by Frameworks Engineer in 618487022

It seems that modifying the cell-accessories after the cell has returned (via the async completion handler) does not trigger a view update of said cell. 

Setting a new array of cell accessories to the accessories property on UICollectionViewListCell will update the visible accessories in the cell, and cause the cell to internally layout its subviews again. You can even set the accessories in an animation to animate the change.

However, there are a few things to keep in mind:
  • When you set an array of accessories, the array of accessories is copied by the cell, which means you need to set a new array to the property again to make any changes to any accessories.

  • Changing the accessories may change the height required for the cell (e.g. if more accessories are visible, less space is available for your cell content, which may cause text to wrap to multiple lines, etc). As with changes you make to any properties on the cell directly, changing the accessories will not cause the item's size (height) to be invalidated in the collection view. If you want to resize the cell, you need to invalidate the collection view layout to recompute the size.

Finally, just to clarify, setting new accessories on the cell will not trigger the cell registration closure to be called again. That is only called when you dequeue a cell using the registration, in response to the collection view requesting a cell during scrolling, or after a reload or updates to the collection view.
Accepted Answer

It seems that modifying the cell-accessories after the cell has returned (via the async completion handler) does not trigger a view update of said cell. 

Setting a new array of cell accessories to the accessories property on UICollectionViewListCell will update the visible accessories in the cell, and cause the cell to internally layout its subviews again. You can even set the accessories in an animation to animate the change.

However, there are a few things to keep in mind:
  • When you set an array of accessories, the array of accessories is copied by the cell, which means you need to set a new array to the property again to make any changes to any accessories.

  • Changing the accessories may change the height required for the cell (e.g. if more accessories are visible, less space is available for your cell content, which may cause text to wrap to multiple lines, etc). As with changes you make to any properties on the cell directly, changing the accessories will not cause the item's size (height) to be invalidated in the collection view. If you want to resize the cell, you need to invalidate the collection view layout to recompute the size.

Finally, just to clarify, setting new accessories on the cell will not trigger the cell registration closure to be called again. That is only called when you dequeue a cell using the registration, in response to the collection view requesting a cell during scrolling, or after a reload or updates to the collection view.
Hmm, on beta 1 this appears to work for cells that scroll in from off screen, but not cells that are visible on initial load...

Additional Oddities:
If I drag and drop a cell that has my custom accessory, or does not have the accessory but should, I observe that the accessory momentarily appears on the cell and then disappears after a short delay.

The custom View in this case is just a UIButton with an SF Symbol "info.circle"

Found my mistake. My closure that was updating the cell was updating a stale cell that had since been replaced.

Updating accessories on cells works great - Just gotta do it on the right cell.
Updating Cell Accessories after Configure?
 
 
Q