Ios 配置具有相同单元格类型但不同UI样式的UICollectionViewCells
我有一个具有相同单元格类型的Ios 配置具有相同单元格类型但不同UI样式的UICollectionViewCells,ios,swift,user-interface,uicollectionview,uicollectionviewcell,Ios,Swift,User Interface,Uicollectionview,Uicollectionviewcell,我有一个具有相同单元格类型的UICollectionView,但每个单元格都有不同的UI属性,如background color,textColor 我可以在collectionView(\uCollectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell中配置它们,如下所示: func collectionView(_ collectionView: UICollectionVie
UICollectionView
,但每个单元格都有不同的UI属性,如background color
,textColor
我可以在collectionView(\uCollectionView: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
}
因此,我必须从我的模型中提取数据,并将其与UI属性一起放在一个新对象中?或者更改您的模型以包含这些UI属性