I have a DisclosureGroup(isExpanded: ..) inside a SwiftUI view that shows checkbox options with a button that when clicked shows an Alert for the user to accept/store their selection. The SwiftUI view is inside a tableView cell using cell.contentConfiguration = UIHostingConfiguration {..}
- The Alert action from the SwiftUI view has to call a function to store the user’s choice that is located in the Tableview controller
- it will also have to delete the current row updating the tableview UI immediately.
The data on the SwiftUI View are sent by TableViewController using a class ShowContent: ObservableObject {.. through @Published properties.
- Question:
How can I make a trigger action to update the UIKit UI coming from the SwiftUI View Alert to make the tableView be updated asap?
The talk Use SwiftUI with UIKit SwiftUI with UIKit] and sample Using SwiftUI with UIKit cover this topic.
Traditionally, when using UIKit, you would need to manually tell the diffable data source about these changes by reconfiguring or reloading items in the snapshot. But when using SwiftUI in cells, this isn't necessary anymore. By storing the ObservableObject model in an ObservedObject property in our SwiftUI view, changes to published properties of the model will automatically trigger SwiftUI to refresh the view. This establishes a direct connection between the model and the SwiftUI view inside of the cell. When a change is made, the SwiftUI views in the cell are updated directly, without going through the diffable data source or the UICollectionView. When a cell's data changes, it may cause the cell to need to grow or shrink to fit the new content. But if the SwiftUI cell content is being updated directly without going through UIKit, how does the collection view know to resize the cell? UIHostingConfiguration takes advantage of a brand-new feature in UIKit to make this work.
Hopefully this helps. RIco