swiftuicollectionviewuikitdiffabledatasource

How to know when a collection view outline has been expanded?


I am using an outline view style UICollectionView setup and setting up my header cells like so:

var content = cell.defaultContentConfiguration()
content.text = brand.name ?? "Unknown"
cell.contentConfiguration = content
  
let headerDisclosureOption = UICellAccessory.OutlineDisclosureOptions(style: .header)
cell.accessories = [.outlineDisclosure(options: headerDisclosureOption)]

However is it possible to receive something like a delegate call whenever a cell section is expanded? I would like to pull fresh data from the internet whenever a list is expanded.


Solution

  • Use the diffable data source's sectionSnapshotHandlers property. It is a SectionSnapshotHandlers instance, a struct consisting entirely of properties whose values are functions. Those functions, which you assign, are your callbacks.

    See https://developer.apple.com/documentation/uikit/uicollectionviewdiffabledatasource/3600966-sectionsnapshothandlers