-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductsTableViewController.swift
61 lines (49 loc) · 1.92 KB
/
ProductsTableViewController.swift
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
54
55
56
57
58
59
60
61
//
// ProductsTableViewController.swift
// GoodAsOldPhones
//
// Copyright © 2016 Code School. All rights reserved.
//
import UIKit
class ProductsTableViewController: UITableViewController {
private var products: [Product]?
private let identifer = "productCell"
override func viewDidLoad() {
super.viewDidLoad()
products = [
Product(name: "1907 Wall Set", cellImageName: "image-cell1", fullscreenImageName: "phone-fullscreen1"),
Product(name: "1921 Dial Phone", cellImageName: "image-cell2", fullscreenImageName: "phone-fullscreen2"),
Product(name: "1937 Desk Set", cellImageName: "image-cell3", fullscreenImageName: "phone-fullscreen3"),
Product(name: "1984 Moto Portable", cellImageName: "image-cell4", fullscreenImageName: "phone-fullscreen4")
]
}
// MARK: - View Transfer
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showProduct" {
if let cell = sender as? UITableViewCell,
let indexPath = tableView.indexPath(for: cell),
let productVC = segue.destination as? ProductViewController {
productVC.product = products?[indexPath.row]
}
}
}
}
// MARK: - UITableViewDataSource
extension ProductsTableViewController {
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int
{
return products?.count ?? 0
}
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: identifer, for: indexPath)
guard let products = products else { return cell }
cell.textLabel?.text = products[indexPath.row].name
if let imageName = products[indexPath.row].cellImageName {
cell.imageView?.image = UIImage(named: imageName)
}
return cell;
}
}