-
Notifications
You must be signed in to change notification settings - Fork 0
/
BannerCell.swift
executable file
·165 lines (93 loc) · 4.87 KB
/
BannerCell.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// CategoryCell.swift
// LiveTVAppStoreScratch
//
// Created by Shashwat Singh on 3/9/17.
// Copyright © 2017 Shashanoid. All rights reserved.
//
import UIKit
class BannerCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
var categories: [Category]?
var featuredAppsController: CategoryController?
override init(frame: CGRect) {
super.init(frame: frame)
setupviews()
setupchannels()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
let nameLabel: UILabel = {
let label = UILabel()
label.text = "Categories"
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.systemFont(ofSize: 16)
return label
}()
let appsCollectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.showsHorizontalScrollIndicator = false
collectionView.backgroundColor = .white
return collectionView
}()
func setupviews(){
addSubview(appsCollectionView)
appsCollectionView.dataSource = self
appsCollectionView.delegate = self
appsCollectionView.register(CategoryInfoCell.self, forCellWithReuseIdentifier: "cellId")
// addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":nameLabel]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":appsCollectionView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0":appsCollectionView]))
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = categories?.count{
return count
}
return 0
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! CategoryInfoCell
cell.nameLabel.text = categories?[indexPath.item].name
cell.priceLabel.text = categories?[indexPath.item].language
cell.category = categories?[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 100, height: 160)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 7, left: 5, bottom: 0, right: 7)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let imageurl = categories?[indexPath.item].categoryImageURL
print(imageurl)
}
func setupchannels(){
guard let url = URL(string: "https://api.myjson.com/bins/620c1") else {return}
URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil{
print(error as Any)
return
}
guard let processdata = data else {return}
do{
let json = try(JSONSerialization.jsonObject(with: processdata, options: .mutableContainers))
guard let dictionaries = json as? [[String:Any]] else {return}
self.categories = []
for dictionary in dictionaries{
let category = Category(dictionary: dictionary)
self.categories?.append(category)
}
DispatchQueue.main.async {
self.appsCollectionView.reloadData()
}
}catch{
//print error
}
}.resume()
}
}