gpt4 book ai didi

ios - 配置具有相同单元格类型但不同 UI 样式的 UICollectionView 单元格

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

我有一个具有相同单元格类型的 UICollectionView,但每个单元格都有不同的 UI 属性,例如 background colortextColor ...

我可以在 collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 中配置它们,如下所示:

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let item = indexPath.item
switch item {
case 1:
let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as ACollectionViewCell
cell.ScoreName.text = xxx
cell.ScoreName.textColor = xxx
cell.score.text = xxx
cell.score.backgroundColor = xxx
cell.....

return cell
case 2: ...
case 3: ...
case 4: ...
case 5: ...
case 6:
let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as BCollectionViewCell
cell.title.text = "xxx xxx"
cell.score.isHidden = true
return cell
default:

let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as DCollectionViewCell
cell.title.text = "xxx"
cell.score.text = "xx"
cell.score.backgroundColor = .green
cell....
return cell
}
}

正如您从案例 1 到案例 5 中看到的具有不同 UI 属性的相同单元格类型,它根本没有吸引力,也不可扩展,那么处理此类配置的最佳方法是什么?

最佳答案

您需要将数据保存在内存中,因此由于 UICollectionViewCell 的可重用特性,您必须存储单元格的配置。

let dictForCell = ["color":UIColor.redColor(),"isHidden":false] //add key value according to your need
let arrOfCell = [dictForCell] //also add dictionary of detail

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let dictData = [indexPath.row]
let cell = collectionView.dequeueReusableCell(forIndexPath: indexPath) as ACollectionViewCell
cell.ScoreName.textColor = dictData["color"] as! UIColor
cell.score.isHidden = dictData["color"] as! Bool
cell.score.text = xxx
cell.score.backgroundColor = xxx
cell.ScoreName.text = xxx
return cell
}

关于ios - 配置具有相同单元格类型但不同 UI 样式的 UICollectionView 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43117470/

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