I am new in swift .I have CollectionView in side tableView.I took code from this link. .I have modified it by adding the new CollectionView xib file and attached the corresponding swift files to new xib file . I have successfully initialised image and label in first TableView cell
struct Colors {
var objectsArray = [
TableViewCellModel(
category: "ALL DEALS",
headerButton: UIButton.init(),
colors: [
[CollectionViewCellModel(image: UIImage(named:"Rectangle1x.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",
colors: [
// SubCategory #2.1
[CollectionViewCellModelButton(collectionButton:UIButton.init()),
CollectionViewCellModelButton(collectionButton:UIButton.init()),
CollectionViewCellModelButton(collectionButton:UIButton.init()),
CollectionViewCellModelButton(collectionButton:UIButton.init())]
])
]
}
Here is my UICollectionViewCell
import UIKit
class CollectionViewCellButton: UICollectionViewCell {
@IBOutlet var collectionButton: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
}
Here is my Model struct
import Foundation
import UIKit
struct CollectionViewCellModelButton {
var collectionButton: UIButton!
}
How to initialise the button in second tableView Cell with name in CollectionViewCellModelButton?
Line CollectionViewCellModelButton(collectionButton:UIButton.init())
is giving syntax error how to correct it ?
Error is
Cannot convert value of type ‘CollectionViewCellModelButton’ to
expected element type
‘Array.ArrayLiteralElement’ (aka
‘CollectionViewCellModel’)
2
Answers
Your UICollectionViewCell already has a UIButton outlet which will be initialized when cell loads
Here is the modified model, which you can use
Edit: The error you are getting is because you are passing data type CollectionViewCellModelButton to a type which is expecting CollectionViewCellModel
To fix this issue, you can create a common protocol which you can conform to both your models
Now passing CollectionViewCellModelButton won’t give error
But you have to downcast your model while using it like this
Similar downcasting should be done while using CollectionViewCellModel also
The value color that is in your TableViewCellModel is of type CollectionViewCellModel i.e the dataType of variable color is [CollectionViewCellModel] and you are trying to pass data of type CollectionViewCellModelButton.