Skip to content

Instantly share code, notes, and snippets.

@karigrooms
Created February 23, 2021 20:01
Show Gist options
  • Save karigrooms/c8375d800118ae85c0e9238c5f0d1558 to your computer and use it in GitHub Desktop.
Save karigrooms/c8375d800118ae85c0e9238c5f0d1558 to your computer and use it in GitHub Desktop.
Blog post: SwiftUI with UICollectionView - 8. Add fixture data to pass to cells
import SwiftUI
import UIKit
class CollectionViewExample: UIViewController {
//...
// MARK: Data
struct Item: MyCollectionViewCell.Content {
var id: String
var imageName: String
var title: String
var description: String
}
let items: [Item] = [
Item(id: UUID().uuidString, imageName: "condos", title: "Condo with awesome views of downtown", description: "$117 avg/night"),
Item(id: UUID().uuidString, imageName: "houses", title: "Oceanfront 3 BR/3 BA", description: "$400 avg/night"),
Item(id: UUID().uuidString, imageName: "studios", title: "Art Studio", description: "$65 avg/night"),
Item(id: UUID().uuidString, imageName: "villas", title: "Luxury 4 BR/3 BA Villa", description: "$109 avg/night"),
Item(id: UUID().uuidString, imageName: "cabins", title: "Cabin in the Pike", description: "$200 avg/night"),
Item(id: UUID().uuidString, imageName: "bungalows", title: "Cottage on Ocean Bluff", description: "$159 avg/night"),
Item(id: UUID().uuidString, imageName: "resorts", title: "Terranea Oceanside King Casita", description: "$351 avg/night"),
Item(id: UUID().uuidString, imageName: "chalets", title: "Romantic Mountain Chalet", description: "$104 avg/night"),
Item(id: UUID().uuidString, imageName: "farmhouses", title: "Cozy Farmhouse on 10 Acres", description: "$199 avg/night")
]
}
// MARK: UICollectionViewDataSource
extension CollectionViewExample: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return items.count
}
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")
}
let item = items[indexPath.row]
cell.embed(in: self, withContent: item)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment