我是 swift 的新手。我在 side tableView 中有 CollectionView。我從這個鏈接中獲取了代碼。.我通過添加新的 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”)
uj5u.com熱心網友回復:
您的 TableViewCellModel 中的值 color 是CollectionViewCellModel型別,即可變顏色的 dataType 是[CollectionViewCellModel]并且您正在嘗試傳遞CollectionViewCellModelButton型別的資料。
uj5u.com熱心網友回復:
您的 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 時也應該進行類似的向下轉換
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/313635.html
標籤:ios 迅速 代码 合适的视图 uicollectionview
