Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- private let listReuseIdentifier = "ListCell"
- private let gridReuseIdentifier = "GridCell"
- class MainListCollectionViewController: UICollectionViewController {
- private let movies = Movie.dummyMovies
- private var layoutOption: LayoutOption = .list
- override func viewDidLoad() {
- super.viewDidLoad()
- setupCollectionView()
- setupNavigationBarItem()
- setupLayout(with: view.bounds.size)
- }
- private func setupCollectionView() {
- collectionView.register(UINib(nibName: "MovieLayoutListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: listReuseIdentifier)
- collectionView.register(UINib(nibName: "MovieLayoutGridCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: gridReuseIdentifier)
- }
- private func setupLayout(with containerSize: CGSize) {
- guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
- return
- }
- switch layoutOption {
- case .list:
- break
- case .largeGrid, .smallGrid:
- break
- }
- collectionView.reloadData()
- }
- private func setupNavigationBarItem() {
- let barButtonItem = UIBarButtonItem(title: "Layout", style: .plain, target: self, action: #selector(layoutTapped(_:)))
- navigationItem.rightBarButtonItem = barButtonItem
- }
- @objc private func layoutTapped(_ sender: Any) {
- let alertController = UIAlertController(title: "Select Layout", message: nil, preferredStyle: .actionSheet)
- alertController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
- alertController.addAction(UIAlertAction(title: "List", style: .default, handler: { (_) in
- self.layoutOption = .list
- }))
- alertController.addAction(UIAlertAction(title: "Large Grid", style: .default, handler: { (_) in
- self.layoutOption = .largeGrid
- }))
- alertController.addAction(UIAlertAction(title: "Small Grid", style: .default, handler: { (_) in
- self.layoutOption = .smallGrid
- }))
- alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
- present(alertController, animated: true, completion: nil)
- }
- }
- extension MainListCollectionViewController {
- override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return movies.count
- }
- override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- switch layoutOption {
- case .list:
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: listReuseIdentifier, for: indexPath) as! MovieLayoutListCollectionViewCell
- let movie = movies[indexPath.item]
- cell.setup(with: movie)
- return cell
- case .largeGrid:
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gridReuseIdentifier, for: indexPath) as! MovieLayoutGridCollectionViewCell
- let movie = movies[indexPath.item]
- cell.setup(with: movie)
- return cell
- case .smallGrid:
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gridReuseIdentifier, for: indexPath) as! MovieLayoutGridCollectionViewCell
- let movie = movies[indexPath.item]
- cell.setup(with: movie)
- return cell
- }
- }
- }
Add Comment
Please, Sign In to add comment