我试图在用户选择单元格时突出显示单元格的背景。 当用户选择单元格时,我已经成功地更改了单元格的边框。
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
addToList.append(objectsArray[indexPath.row])
let cell = collectionView.cellForItem(at: indexPath)
cell?.contentView.layer.borderWidth = 2.0
cell?.contentView.layer.borderColor = UIColor.gray.cgColor
}
虽然我无法更改背景。
这能做到吗? 谢谢!
最佳答案
对于单元格边框,请像这样使用 cell
contentView
:
cell?.contentView.layer.borderWidth = 2.0
cell?.contentView.layer.borderColor = UIColor.gray.cgColor
对于单元格 backgroundColor
颜色,您需要使用:
cell?.backgroundColor = .gray
关于ios - Swift4,选择单元格时的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52276438/