Guest User

Untitled

a guest
Feb 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import UIKit
  2.  
  3. private let listReuseIdentifier = "ListCell"
  4. private let gridReuseIdentifier = "GridCell"
  5.  
  6. class MainListCollectionViewController: UICollectionViewController {
  7.  
  8. private let movies = Movie.dummyMovies
  9. private var layoutOption: LayoutOption = .list
  10.  
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. setupCollectionView()
  14. setupNavigationBarItem()
  15. setupLayout(with: view.bounds.size)
  16. }
  17.  
  18. private func setupCollectionView() {
  19. collectionView.register(UINib(nibName: "MovieLayoutListCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: listReuseIdentifier)
  20. collectionView.register(UINib(nibName: "MovieLayoutGridCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: gridReuseIdentifier)
  21. }
  22.  
  23. private func setupLayout(with containerSize: CGSize) {
  24. guard let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout else {
  25. return
  26. }
  27.  
  28. switch layoutOption {
  29. case .list:
  30. break
  31.  
  32. case .largeGrid, .smallGrid:
  33. break
  34. }
  35.  
  36. collectionView.reloadData()
  37. }
  38.  
  39. private func setupNavigationBarItem() {
  40. let barButtonItem = UIBarButtonItem(title: "Layout", style: .plain, target: self, action: #selector(layoutTapped(_:)))
  41. navigationItem.rightBarButtonItem = barButtonItem
  42. }
  43.  
  44. @objc private func layoutTapped(_ sender: Any) {
  45. let alertController = UIAlertController(title: "Select Layout", message: nil, preferredStyle: .actionSheet)
  46. alertController.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
  47. alertController.addAction(UIAlertAction(title: "List", style: .default, handler: { (_) in
  48. self.layoutOption = .list
  49. }))
  50.  
  51. alertController.addAction(UIAlertAction(title: "Large Grid", style: .default, handler: { (_) in
  52. self.layoutOption = .largeGrid
  53. }))
  54.  
  55. alertController.addAction(UIAlertAction(title: "Small Grid", style: .default, handler: { (_) in
  56. self.layoutOption = .smallGrid
  57. }))
  58.  
  59. alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  60. present(alertController, animated: true, completion: nil)
  61. }
  62. }
  63.  
  64. extension MainListCollectionViewController {
  65.  
  66. override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  67. return movies.count
  68. }
  69.  
  70. override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  71. switch layoutOption {
  72. case .list:
  73. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: listReuseIdentifier, for: indexPath) as! MovieLayoutListCollectionViewCell
  74. let movie = movies[indexPath.item]
  75. cell.setup(with: movie)
  76. return cell
  77.  
  78. case .largeGrid:
  79. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gridReuseIdentifier, for: indexPath) as! MovieLayoutGridCollectionViewCell
  80. let movie = movies[indexPath.item]
  81. cell.setup(with: movie)
  82. return cell
  83.  
  84. case .smallGrid:
  85. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: gridReuseIdentifier, for: indexPath) as! MovieLayoutGridCollectionViewCell
  86. let movie = movies[indexPath.item]
  87. cell.setup(with: movie)
  88. return cell
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment