gpt4 book ai didi

ios - Swift 中的自定义单元格 UICollectionView

转载 作者:行者123 更新时间:2023-11-28 15:24:54 25 4
gpt4 key购买 nike

我正在尝试为我的 collectionView 中的每个索引项创建不同的单元格,并且我试图将它们向下转换为它们各自的单元格以访问它们的变量。下面的代码输出此错误“无法将类型‘LiveCell.Type’的值分配给类型‘UICollectionViewCell’

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier: String
let cellName: UICollectionViewCell

if indexPath.item == 1 {
identifier = LiveCellId
cellName = LiveCell
} else if indexPath.item == 2 {
identifier = LiveCellId
cellName = LiveCell
} else {
identifier = FeedCellId
cellName = FeedCell
}

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! cellName //Trying to downcast cell from the variable called cellName
cell.homeController = self

return cell
}

有解决办法吗?提前致谢

最佳答案

试试这个:-

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let identifier: String
let cellName: UICollectionViewCell?

if indexPath.item == 1 {
identifier = LiveCellId
cellName = LiveCell as? UICollectionViewCell
} else if indexPath.item == 2 {
identifier = LiveCellId
cellName = LiveCell as? UICollectionViewCell
} else {
identifier = FeedCellId
cellName = FeedCell as? UICollectionViewCell
}

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: identifier, for: indexPath) as! cellName //Trying to downcast cell from the variable called cellName
cell.homeController = self

return cell
}

关于ios - Swift 中的自定义单元格 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45407122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com