Skip to content

Instantly share code, notes, and snippets.

@karigrooms
Created February 23, 2021 19:16
Show Gist options
  • Save karigrooms/3e9664e6d997b02162b923e5f4778933 to your computer and use it in GitHub Desktop.
Save karigrooms/3e9664e6d997b02162b923e5f4778933 to your computer and use it in GitHub Desktop.
Blog post: SwiftUI with UICollectionView - 5. Add SwiftUI UIHostingController as a child of our UIViewController
//...
extension CollectionViewExample: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 9
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MyCollectionViewCell.reuseIdentifier, for: indexPath) as? MyCollectionViewCell else {
fatalError("Could not dequeue cell")
}
// Add SwiftUI UIHostingController as a child of our UIViewController
cell.embed(in: self)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment