【解決方法】テーブルビューの最初と2番目のセルにxibsを使用して2つの異なるコレクションビューを追加する方法


私はswiftが初めてです。これからコードを取得しました リンク. 最初の「セクション」tableViewに最初のCollectionView(画像の配列)があります。それはそのリンクによって提供されるロジックであるため、正常に機能していました。2番目の新しいcollectionView(つまり配列)を表示するようにコードを変更したいボタンの) tableView の 2 番目の「セクション」にあるので、ビュー コントローラーは次のようになります。

この絵

このために、コードに次の変更を加えました:

1- 両方の CollectionView モデルに準拠できる共通のプロトコルを作成しました

迅速
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-Colors 配列を変更して、両方の 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- TableViewCell.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()
        }

でプロトコルを正しく使用する方法 cellForItemAnd indexPath セルに値を代入し、 コメントの誤りを修正.

私が試したこと:

コードはこちらからダウンロードできます グーグルドライブリンク バッターの視覚化のためにそれを実行し、何が必要かを確認します(ここでは、コードが出力を表示できるようにこれらの変更をコメントしました)?

解決策 1

<<<<<>>>>>>>

<pre lang="Swift"><pre lang="Swift">

コメント

タイトルとURLをコピーしました