Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

Swift
protocol CollectionViewModel {
}

struct CollectionViewCellModelButton: CollectionViewModel {
    var collectionButton: UIButton!
}

struct CollectionViewCellModel: CollectionViewModel {
       var image: UIImage
        var dicountAmountLabel: String
        var dicountLabel: String
        var customerTypeLabel: String
}

// pass the protocol to colors
struct TableViewCellModel {
    var category: String
    var headerButton: UIButton!
    var colors: [[CollectionViewModel]]

}

2-I have changed Colors array to initialise both collectionViews

Swift
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
Swift
class TableViewCell: UITableViewCell {
   //  var rowWithColors: [CollectionViewCellModel]?  //previous
    var rowWithColors: [CollectionViewModel]?
}
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellid", for: indexPath) as? CollectionViewCell {                

//error:Reference to member 'dequeueReusableCell' cannot be resolved without a contextual type

            if let model = self.rowWithColors?[indexPath.item] as? CollectionViewCellModel {

            model.imageView.image = self.rowWithColors?[indexPath.item].image

//error:Type of expression is ambiguous without more context

            model.dicountAmountLabel.text = self.rowWithColors?[indexPath.item].dicountAmountLabel ?? ""

//error:Value of type 'CollectionViewModel' has no member 'dicountAmountLabel'
//error2:Value of type 'String' has no member 'text'

            model.dicountLabel.text = self.rowWithColors?[indexPath.item].dicountLabel ?? ""

//error:Value of type 'CollectionViewModel' has no member 'dicountLabel'
//error2:Value of type 'String' has no member 'text'

            model.customerTypeLabel.text = self.rowWithColors?[indexPath.item].customerTypeLabel ?? ""

//error:Value of type 'CollectionViewModel' has no member 'dicountLabel'
//error2:Value of type 'String' has no member 'text'
            
    return cell
            }
            
        }
       if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellButtonid", for: indexPath) as? CollectionViewCellButton {
     
//error: Reference to member 'dequeueReusableCell' cannot be resolved without a contextual type

         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)?
Posted
Updated 22-Feb-23 20:41pm
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900