Skip to content

Instantly share code, notes, and snippets.

@srstanic
Created February 28, 2023 12:47
Show Gist options
  • Save srstanic/25b5b718e5a925132a283742517256b7 to your computer and use it in GitHub Desktop.
Save srstanic/25b5b718e5a925132a283742517256b7 to your computer and use it in GitHub Desktop.
MyCollectionViewController #1
import UIKit
struct CollectionItemModel {
let title: String?
let image: UIImage?
}
/// First set `model` and then present on screen.
final class MyCollectionViewController: UICollectionViewController {
var models: [CollectionItemModel]?
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let model = models?[indexPath.row]
if let image = model?.image {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "image", for: indexPath)
cell.largeContentImage = image
return cell
} else if let title = model?.title {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "text", for: indexPath)
cell.largeContentTitle = title
return cell
}
return UICollectionViewCell()
}
}
func createMyCollectionViewController() -> MyCollectionViewController {
let viewController = MyCollectionViewController()
let models = [
CollectionItemModel(title: nil, image: UIImage(named: "what")),
CollectionItemModel(title: "Text", image: nil)
]
viewController.models = models
return viewController
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment