MyRewardsOffersScrollTableViewCell.swift
6.72 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
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
162
163
164
165
166
//
// MyRewardsOffersScrollTableViewCell.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 23/5/25.
//
import UIKit
protocol MyRewardsOffersScrollTableViewCellDelegate: AnyObject {
func didSelectOffer(_ offer: OfferModel)
}
@objc public class MyRewardsOffersScrollTableViewCell: UITableViewCell {
@IBOutlet weak var categoryLabel: UILabel!
@IBOutlet weak var allButtonView: UIView!
@IBOutlet weak var allButtonLabel: UILabel!
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
weak var delegate: MyRewardsOffersScrollTableViewCellDelegate?
var data: SectionModel?
public override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
allButtonView.layer.borderWidth = 1.0
allButtonView.layer.borderColor = UIColor(rgb: 0x000F1E).cgColor
allButtonView.layer.cornerRadius = 4.0
allButtonLabel.text = "Όλα"
allButtonLabel.font = UIFont(name: "PingLCG-Regular", size: 16)
allButtonLabel.textColor = UIColor(rgb: 0x00111B)
allButtonLabel.frame.size.width = allButtonLabel.intrinsicContentSize.width
allButtonLabel.frame.size.height = allButtonLabel.intrinsicContentSize.height
// Register XIBs for collection view cells
collectionView.register(UINib(nibName: "MyRewardsOfferCollectionViewCell", bundle: Bundle(for: MyEmptyClass.self)), forCellWithReuseIdentifier: "MyRewardsOfferCollectionViewCell")
// Fix background colors
collectionView.backgroundColor = UIColor.clear
self.backgroundColor = UIColor.clear
self.contentView.backgroundColor = UIColor.clear
// Remove content insets and gaps
collectionView.contentInset = UIEdgeInsets.zero
collectionView.scrollIndicatorInsets = UIEdgeInsets.zero
if #available(iOS 11.0, *) {
collectionView.contentInsetAdjustmentBehavior = .never
}
// Configure collection view layout
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.scrollDirection = .horizontal
layout.minimumLineSpacing = 7
layout.minimumInteritemSpacing = 0
layout.sectionInset = UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
}
// Enable paging for smooth banner scrolling
// collectionView.isPagingEnabled = true
collectionView.showsHorizontalScrollIndicator = false
// Set delegates
collectionView.delegate = self
collectionView.dataSource = self
}
public override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
func configureCell(data: SectionModel?) {
self.data = data
// Update collection view height based on section
if data?.title == "Αγαπημένα" {
collectionViewHeightConstraint.constant = 350 // Match cell height
} else {
collectionViewHeightConstraint.constant = 232 // Default height
}
// Configure layout based on section type
if let layout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
if data?.title == "Αγαπημένα" {
layout.minimumLineSpacing = 24
} else {
layout.minimumLineSpacing = 7
}
}
let catBoldText = (data?.title ?? "") + " "
let catRegText = "(" + String(data?.count ?? 0) + ")"
let attrBold = [NSAttributedString.Key.font : UIFont(name: "PingLCG-Bold", size: 18) ?? UIFont.boldSystemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor(rgb: 0x000F1E)]
let attrRegular = [NSAttributedString.Key.font : UIFont(name: "PingLCG-Regular", size: 18) ?? UIFont.systemFont(ofSize: 17), NSAttributedString.Key.foregroundColor: UIColor(rgb: 0x000F1E)]
let catAttributedString = NSMutableAttributedString(string:catBoldText, attributes:attrBold)
let catRegString = NSMutableAttributedString(string: catRegText, attributes:attrRegular)
catAttributedString.append(catRegString)
categoryLabel.attributedText = catAttributedString
self.collectionView.reloadData();
}
}
extension MyRewardsOffersScrollTableViewCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
public func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.data?.offers.count ?? 0
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyRewardsOfferCollectionViewCell", for: indexPath) as! MyRewardsOfferCollectionViewCell
// cell.configureCell(offer: self.data?.offers[indexPath.row])
if let offer = self.data?.offers[indexPath.row] {
cell.configureCell(data: offer)
}
return cell;
}
public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
if let offer = self.data?.offers[indexPath.row] {
delegate?.didSelectOffer(offer)
}
}
// MARK: - UICollectionViewDelegateFlowLayout
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if self.data?.title == "Αγαπημένα" {
return CGSize(width: 275, height: 350)
} else {
return CGSize(width: 257, height: 232)
}
}
// Distance Between Item Cells
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
if self.data?.title == "Αγαπημένα" {
return 24
} else {
return 7
}
}
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
// Cell Margin
public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 24, bottom: 0, right: 24)
}
}