Skip to content

Instantly share code, notes, and snippets.

@wingchi
Last active May 5, 2018 22:40
Show Gist options
  • Save wingchi/877e68ee6a0dc26b61335cc8c4bce2ba to your computer and use it in GitHub Desktop.
Save wingchi/877e68ee6a0dc26b61335cc8c4bce2ba to your computer and use it in GitHub Desktop.
View Controller with Collection View boilerplate
import UIKit
class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!
private let gridViewModel = GridViewModel()
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
func setupCollectionView() {
collectionView.dataSource = self
collectionView.delegate = self
collectionView.register(GridCollectionViewCell.self, forCellWithReuseIdentifier: GridCollectionViewCell.nibName)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GridCollectionViewCell.nibName, for: indexPath)
as? GridCollectionViewCell
else { fatalError("Could not find GridCollectionViewCell") }
let labelString = "\(gridViewModel.data[indexPath.row])"
cell.configure(with: labelString)
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return gridViewModel.data.count
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 100)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment