【发布时间】:2017-11-30 05:01:15
【问题描述】:
我在UICollectionView 的UICollectionViewCell(自定义类)中有一个按钮。单击按钮时,它会更改其背景颜色。一开始效果很好,但是当我更改主视图的背景颜色时,我无法更新按钮的背景颜色。背景颜色存储在一个数组中,这是单元格设置函数:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("Rebuilding cells")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ColorCollectionViewCell
cell.button.tappedColor = backgroundColors[section]
cell.button.originalColor = backgroundColors[section + 1]
cell.button.delegate = self
cell.button.addTarget(self, action: #selector(didHitButton), for: .touchUpInside)
return cell
}
因此,如果我以 section 作为 1 开始,它会加载具有正确背景颜色的单元格按钮(黑色是部分数组中的第一种颜色。然后,我将计数设置为零和 grid.reloadData() 成功清除细胞。
但是当我将section 设置为 2 并再次重置总单元格计数时,单元格不会采用新的背景颜色(第 2 节颜色为绿色)。从第 1 部分开始,它们保持黑色。
当数据更新时,我将如何告诉UICollectionView 重新绘制单元格中的按钮背景颜色,其中背景颜色来自基于index 变量的数组?
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell