This file contains hidden or 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 SwiftUI | |
import UIKit | |
class MyCollectionViewCell: UICollectionViewCell { | |
static var reuseIdentifier = "MyCollectionViewCell" | |
lazy var host: UIHostingController = { | |
return UIHostingController(rootView: Card()) | |
}() |
This file contains hidden or 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 SwiftUI | |
import UIKit | |
class CollectionViewExample: UIViewController { | |
lazy var collectionView: UICollectionView = { | |
let layout = UICollectionViewFlowLayout() | |
layout.scrollDirection = .horizontal | |
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout) | |
collectionView.backgroundColor = UIColor(named: "Light") |
This file contains hidden or 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 SwiftUI | |
import UIKit | |
class MyCollectionViewCell: UICollectionViewCell { | |
lazy var host: UIHostingController = { | |
return UIHostingController(rootView: Card()) | |
}() | |
//... |
This file contains hidden or 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
//... | |
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 { |
This file contains hidden or 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 SwiftUI | |
protocol CardContent { | |
var imageName: String { get } | |
var title: String { get } | |
var description: String { get } | |
} | |
struct Card: View { | |
This file contains hidden or 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 SwiftUI | |
import UIKit | |
class MyCollectionViewCell: UICollectionViewCell { | |
static var reuseIdentifier = "MyCollectionViewCell" | |
typealias Content = Card.Content | |
private(set) var host: UIHostingController<Card>? |
This file contains hidden or 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 SwiftUI | |
import UIKit | |
class CollectionViewExample: UIViewController { | |
//... | |
// MARK: Data | |
struct Item: MyCollectionViewCell.Content { |
This file contains hidden or 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 SwiftUI | |
import UIKit | |
/// Subclass for embedding a SwiftUI View inside of UICollectionViewCell | |
/// Usage: `class MySwiftUICell: SwiftUICollectionViewCell<Card> { ... }` | |
open class SwiftUICollectionViewCell<Content>: UICollectionViewCell where Content: View { | |
/// Controller to host the SwiftUI View | |
private(set) var host: UIHostingController<Content>? | |
This file contains hidden or 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 SwiftUI | |
import UIKit | |
class MyCollectionViewCell: SwiftUICollectionViewCell<Card> { | |
static var reuseIdentifier = "MyCollectionViewCell" | |
typealias Content = Card.Content | |
func configure(with content: Content, parent: UIViewController) { |
This file contains hidden or 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
// MARK: UICollectionViewDelegateFlowLayout | |
extension CollectionViewExample: UICollectionViewDelegateFlowLayout { | |
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
return CGSize(width: 240, height: 320) | |
} | |
} |