【问题标题】:How to Initiliazed a button in CollectionViewCell Arrary如何初始化 CollectionView 单元格数组中的按钮
【发布时间】:2021-12-02 13:26:50
【问题描述】:

我是 swift 的新手。我在侧 tableView 中有 CollectionView。我从这个 link 获取代码。 .我通过添加新的 CollectionView xib 文件对其进行了修改,并将相应的 swift 文件附加到新的 xib 文件中。我已经在第一个 TableView 单元格中成功初始化了图像和标签

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())]
            ])
    ]
}

这是我的 UICollectionViewCell

import UIKit
    class CollectionViewCellButton: UICollectionViewCell {
        
        @IBOutlet var collectionButton: UIButton!
        
        override func awakeFromNib() {
            super.awakeFromNib()
            // Initialization code
        }
    }

这是我的模型结构

import Foundation
import UIKit

struct CollectionViewCellModelButton {
    var collectionButton: UIButton!
}

如何使用 CollectionViewCellModelButton 中的名称初始化第二个 tableView Cell 中的按钮?

CollectionViewCellModelButton(collectionButton:UIButton.init()) 行出现语法错误如何纠正? 错误是

无法将“CollectionViewCellModelButton”类型的值转换为 预期元素类型 'Array.ArrayLiteralElement'(又名 'CollectionViewCellModel')

【问题讨论】:

    标签: ios swift xcode uitableview uicollectionview


    【解决方案1】:

    您的 TableViewCellModel 中的值颜色是 CollectionViewCellModel 类型,即变量颜色的数据类型是 [CollectionViewCellModel] 并且您正在尝试传递 类型的数据>CollectionViewCellModelButton.

    【讨论】:

      【解决方案2】:

      您的 UICollectionViewCell 已经有一个 UIButton 插座,它将在单元格加载时初始化 这是修改后的模型,你可以使用

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

      编辑:您遇到的错误是因为您将数据类型 CollectionViewCellModelButton 传递给期望 CollectionViewCellModel

      的类型

      要解决此问题,您可以创建一个通用协议,让您的模型都符合该协议

      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]]
      }
      

      现在传递 CollectionViewCellModelButton 不会报错

      但是你必须在像这样使用它时降低你的模型

      // 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()
        }
      

      在使用 CollectionViewCellModel 时也应该进行类似的向下转换

      【讨论】:

      • 请告诉我如何在此代码中添加按钮:TableViewCellModel( category: "CATEGORIES", colors: [ [CollectionViewCellModelButton(collectionButton:UIButton__?_需要添加什么代码?标记地点
      • 请参考编辑后的答案!!
      • 标题不是按钮,它是灰色的“名称标签”...查看我已更改的代码。我有CollectionViewCellModel,它与视觉更改链接中的代码相同。我添加了新的xib,上面提供了有问题的单元格和结构。 CollectionViewCellModelButton 应该使用此代码在第二个 tableView 部分添加按钮。 colors: [ [CollectionViewCellModelButton(collectionButton:UIButton), //How to initialise button ? CollectionViewCellModelButton(collectionButton:UIButton(coder: “text”), ] 。改正了吗?
      • CollectionViewCellModelButton(collectionButton:UIButton(coder: “text”) 给我语法错误更正了吗?
      • 错误是。 “无法将'CollectionViewCellModelButton'类型的值转换为预期的元素类型'Array.ArrayLiteralElement'(又名'CollectionViewCellModel')”......看起来我已经编辑了我的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      相关资源
      最近更新 更多