-
Notifications
You must be signed in to change notification settings - Fork 3
/
ShoeDetailTableViewController.swift
138 lines (107 loc) · 4.54 KB
/
ShoeDetailTableViewController.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// ShoeDetailTableViewController.swift
// Ecommerce
//
// Created by Eric Rosas on 2/13/17.
// Copyright © 2017 EmpireAppDesignz. All rights reserved.
//
import UIKit
class ShoeDetailTableViewController : UITableViewController
{
var shoe: Shoe!
@IBOutlet weak var shoeImagesHeaderView: ShoeImagesHeaderView!
override func viewDidLoad() {
super.viewDidLoad()
title = shoe.name
self.tableView.estimatedRowHeight = self.tableView.rowHeight
self.tableView.rowHeight = UITableView.automaticDimension
self.tabBarController?.tabBar.items![1].badgeValue = "2"
}
struct Storyboard {
static let showImagesPageVC = "ShowImagesPageViewController"
static let shoeDetailCell = "ShoeDetailCell"
static let productDetailCell = "ProductDetailsCell"
static let suggestionCell = "SuggestionCell"
static let buyButtonCell = "BuyButtonCell"
}
// MARK : - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == Storyboard.showImagesPageVC {
if let imagesPageVC = segue.destination as? ShoeImagesPageViewController {
imagesPageVC.images = shoe.images
imagesPageVC.pageViewControllerDelegate = shoeImagesHeaderView
}
}
}
}
extension ShoeDetailTableViewController
{
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 0 - shoe detail cell
// 1 - buy button
// 2 - shoe full details button cell
// 3 - you might like this cell
return 4
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.shoeDetailCell, for: indexPath) as! ShoeDetailCell
cell.shoe = shoe
cell.selectionStyle = .none
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.buyButtonCell, for: indexPath)
return cell
} else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.productDetailCell, for: indexPath)
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: Storyboard.suggestionCell, for: indexPath) as! SuggestionTableViewCell
return cell
}
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
if indexPath.row == 3 {
if let cell = cell as? SuggestionTableViewCell {
cell.collectionView.dataSource = self
cell.collectionView.delegate = self
cell.collectionView.reloadData()
cell.collectionView.isScrollEnabled = false
}
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 3 {
return self.tableView.bounds.width + 68
} else {
return UITableView.automaticDimension
}
}
}
// MARK: - UICollectionViewDataSource
extension ShoeDetailTableViewController: UICollectionViewDataSource
{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 4
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: Storyboard.suggestionCell, for: indexPath) as! SuggestionCollectionViewCell
let shoes = Shoe.fetchShoes()
cell.image = shoes[indexPath.item].images?.first
return cell
}
}
extension ShoeDetailTableViewController : UICollectionViewDelegate, UICollectionViewDelegateFlowLayout
{
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let layout = collectionViewLayout as! UICollectionViewFlowLayout
layout.minimumLineSpacing = 5.0
layout.minimumInteritemSpacing = 2.5
let itemWidth = (collectionView.bounds.width - 5.0) / 2.0
return CGSize(width: itemWidth, height: itemWidth)
}
}