Created
February 28, 2023 12:47
-
-
Save srstanic/25b5b718e5a925132a283742517256b7 to your computer and use it in GitHub Desktop.
MyCollectionViewController #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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