skip to Main Content

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


  1. Your UICollectionViewCell already has a UIButton outlet which will be initialized when cell loads
    Here is the modified model, which you can use

    struct CollectionViewCellModelButton {
        var btnTitle: String
    }
    
    // Initialize your model like this
    TableViewCellModel(category: "CATEGORIES", colors: [CollectionViewCellModelButton(btnTitle: "text"), CollectionViewCellModelButton(btnTitle: "text1")])
    

    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

    protocol CollectionViewModel {
    }
    
    struct CollectionViewCellModelButton: CollectionViewModel {
        var btnTitle: String
    }
    
    struct CollectionViewCellModel: CollectionViewModel {
        var titleLabel: String
    }
    
    // pass the protocol to colors
    struct TableViewCellModel {
        var category: String
        var subcategory: [String]
        var colors: [[CollectionViewModel]]
    }
    

    Now passing CollectionViewCellModelButton won’t give error

    But you have to downcast your model while using it like this

    // set title in button like this
     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionviewcellid", for: indexPath) as? CollectionViewCell {
       // downcast CollectionViewModel to CollectionViewCellModelButton type
       if let model = self.rowWithColors[indexPath.item] as? CollectionViewCellModelButton {
           cell.collectionButton.setTitle(model.title, for: .normal)
        }
         return cell
        }
        return UICollectionViewCell()
      }
    

    Similar downcasting should be done while using CollectionViewCellModel also

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search