【发布时间】:2019-01-09 02:34:08
【问题描述】:
在我的项目中,我有一个 UICollectionView。在 UICollectionView 中,我有一个自定义单元格。
当在“didSelectItemAt”中选择单元格值时,我可以打印它,但是,如果我尝试在此方法中以任何方式编辑单元格,它不会改变。
我确定我遗漏了一些东西,任何帮助将不胜感激!
@IBOutlet weak var collectionView: UICollectionView!
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return statValues.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCollectionViewCell", for: indexPath) as! customCollectionViewCell
cell.statLabel.text = statHeaders[indexPath.row]
cell.statLabel.textColor = UIColor(red:0.31, green:0.31, blue:0.31, alpha:1.0)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCollectionViewCell", for: indexPath) as! customCollectionViewCell
print(cell.statLabel.text)
cell.backgroundColor = UIColor.yellow
collectionView.reloadData()
}
当用户选择一个单元格时,代码会正确打印 indexPath 的值,但是 backgroundColor 不会改变。
【问题讨论】:
标签: swift uicollectionview uicollectionviewcell