In iOS 16, Apple added UIHostingConfiguration, making it straightforward to use a SwiftUI view in instances of UICollectionViewCell and UITableViewCell. This gist shows how UIHostingConfiguration can be backported to iOS 14 by implementing a type that conforms to UIContentConfiguration.
Starting from iOS 16, we can use SwiftUI views in an instance of UICollectionView or UITableViewCell like this:
cell.contentConfiguration = UIHostingConfiguration {
ExampleCellContentView(color: Color(item.color), text: item.text)
}
With the SwiftUIHostingConfiguration, we can use a similar API starting from iOS 14.
cell.contentConfiguration = SwiftUIHostingConfiguration(parentViewController: self) {
ExampleCellContentView(color: Color(item.color), text: item.text)
}
We pass an instance of our view controller because, under the hood, the SwiftUIHostingConfiguration creates a UIHostingController and inserts it into the view controller hierarchy.