I am new in swift.I took code from this
link. I have first CollectionView(that is array of images) in side first `section` tableView.That was working fine as that was logic provided by that link.I want to change the code so that i will display second new collectionView(that is array of buttons) in second `section` of tableView .So that my view controller look like in
this picture
For this i made following changes into code :
1- I created a common protocol which you can conform to both CollectionView models
protocol CollectionViewModel {
}
struct CollectionViewCellModelButton: CollectionViewModel {
var collectionButton: UIButton!
}
struct CollectionViewCellModel: CollectionViewModel {
var image: UIImage
var dicountAmountLabel: String
var dicountLabel: String
var customerTypeLabel: String
}
struct TableViewCellModel {
var category: String
var headerButton: UIButton!
var colors: [[CollectionViewModel]]
}
2-I have changed Colors array to initialise both collectionViews
struct Colors {
var objectsArray = [
TableViewCellModel(
category: "ALL DEALS",
headerButton: UIButton.init(),
colors: [
[CollectionViewCellModel(image: UIImage(named:"Rectangle.png")!, dicountAmountLabel: "30%", dicountLabel: "Discount", customerTypeLabel: "For All HBL Customers"),
CollectionViewCellModel(image: UIImage(named:"Rectangle2.png")!, dicountAmountLabel: "30%", dicountLabel: "Discount", customerTypeLabel: "For All HBL Customers")]
]),
TableViewCellModel(
category: "CATEGORIES",
headerButton: UIButton.init(),
colors: [
[CollectionViewCellModelButton(collectionButton:UIButton.init()),
CollectionViewCellModelButton(collectionButton:UIButton.init()),
CollectionViewCellModelButton(collectionButton:UIButton.init()),
CollectionViewCellModelButton(collectionButton:UIButton.init())]
]
)
]
}
3- In TableViewCell.swift file i have changed
class TableViewCell: UITableViewCell {
var rowWithColors: [CollectionViewModel]?
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellid", for: indexPath) as? CollectionViewCell {
if let model = self.rowWithColors?[indexPath.item] as? CollectionViewCellModel {
model.imageView.image = self.rowWithColors?[indexPath.item].image
model.dicountAmountLabel.text = self.rowWithColors?[indexPath.item].dicountAmountLabel ?? ""
model.dicountLabel.text = self.rowWithColors?[indexPath.item].dicountLabel ?? ""
model.customerTypeLabel.text = self.rowWithColors?[indexPath.item].customerTypeLabel ?? ""
return cell
}
}
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellButtonid", for: indexPath) as? CollectionViewCellButton {
if let model = self.rowWithColors?[indexPath.item] as? CollectionViewCellModelButton {
model.collectionButton.setTitle("Hi", for: .normal)
model.collectionButton.titleLabel?.text = "Hi"
return cell
}
return UICollectionViewCell()
}
How to correctly use the protocol in
cellForItemAt indexPath and assign values to cell and
correcting error in comment.
What I have tried:
You can download the code from this
google drive link run it for batter visualisation and see what is required(here i commented these changes so that code can be display output)?