-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShoppingBagTableViewController.swift
112 lines (88 loc) · 3.4 KB
/
ShoppingBagTableViewController.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// ShoppingBagTableViewController.swift
// Ecommerce
//
// Created by Eric Rosas on 2/13/17.
// Copyright © 2017 EmpireAppDesignz. All rights reserved.
//
import UIKit
class ShoppingBagTableViewController: UITableViewController
{
struct Storyboard {
static let numberOfItemsCell = "numberOfItemsCell" // cell 0
static let itemCell = "itemCell" // cell 1
static let cartDetailCell = "cartDetailCell" // cell 2
static let cartTotalCell = "cartTotalCell" // cell 3
static let checkoutButtonCell = "checkoutButtonCell" // cell 4
}
var shoes: [Shoe]? = Shoe.fetchShoes()
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableView.automaticDimension
}
}
extension ShoppingBagTableViewController
{
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let shoes = shoes {
return shoes.count + 4
} else {
return 1
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
guard let shoes = shoes else {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.numberOfItemsCell, for: indexPath) as! NumberOfItemsCell
cell.numberOfItemsLabel.text = "\(0) ITEM"
return cell
}
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.numberOfItemsCell, for: indexPath) as! NumberOfItemsCell
cell.numberOfItemsLabel.text = "\(shoes.count) ITEMS"
return cell
} else if indexPath.row == shoes.count + 1 {
// cartDetailCell
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cartDetailCell, for: indexPath)
return cell
} else if indexPath.row == shoes.count + 2 {
// cartTotalCell
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.cartTotalCell, for: indexPath)
return cell
} else if indexPath.row == shoes.count + 3 {
// checkoutButtonCell
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.checkoutButtonCell, for: indexPath)
return cell
} else {
// itemCell
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.itemCell, for: indexPath) as! ShoppingCartItemCell
cell.shoe = shoes[indexPath.row - 1]
return cell
}
}
}
class NumberOfItemsCell: UITableViewCell
{
@IBOutlet weak var numberOfItemsLabel: UILabel!
}
class ShoppingCartItemCell : UITableViewCell
{
@IBOutlet weak var productImageView: UIImageView!
@IBOutlet weak var productNameLabel: UILabel!
@IBOutlet weak var productPriceLabel: UILabel!
@IBOutlet weak var removeButton: UIButton!
var shoe: Shoe! {
didSet {
self.updateUI()
}
}
func updateUI()
{
productImageView.image = shoe.images?.first
productNameLabel.text = shoe.name
productPriceLabel.text = "$\(shoe.price!)"
}
@IBAction func removeButtonDidTap(_ sender: Any) {
}
}