-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlanetsCollectionViewController.swift
More file actions
53 lines (42 loc) · 1.87 KB
/
PlanetsCollectionViewController.swift
File metadata and controls
53 lines (42 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//
// CollectionViewController.swift
// SolarSystem
//
// Created by Neha Thakore on 11/6/17.
// Copyright © 2017 Neha Thakore. All rights reserved.
//
import UIKit
class PlanetsCollectionViewController: UIViewController {
@IBOutlet weak var planetsCollectionView: UICollectionView!
fileprivate var itemsForCollectionView = [PlanetDetails]()
override func viewDidLoad() {
super.viewDidLoad()
for index in 0...20 {
let item = PlanetDetails(planetName: "\(index)")
itemsForCollectionView.append(item)
}
planetsCollectionView.dataSource = self
planetsCollectionView.delegate = self
}
}
extension PlanetsCollectionViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let detailsController = storyboard.instantiateViewController(withIdentifier: "PlanetDetailsViewController") as? PlanetDetailsViewController {
present(detailsController, animated: true, completion: nil)
}
}
}
extension PlanetsCollectionViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return itemsForCollectionView.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PlanetsCollectionViewCell", for: indexPath) as? PlanetsCollectionViewCell else {
return UICollectionViewCell()
}
let details = itemsForCollectionView[indexPath.item]
cell.planetNameLabel.text = details.planetName
return cell
}
}