Last active
May 5, 2018 22:40
-
-
Save wingchi/877e68ee6a0dc26b61335cc8c4bce2ba to your computer and use it in GitHub Desktop.
View Controller with Collection View boilerplate
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 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