繁体   English   中英

Swift4,选定单元格时的背景色

[英]Swift4, Background Color when cell is selected

我试图在用户选择单元格时突出显示其背景。 当用户选择一个单元格时,我已经成功地更改了它的边框。

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

尝试这个。

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    cell.contentView.backgroundColor = UIColor.red
}

func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {
    let cell = collectionView.cellForItem(at: indexPath)
    cell.contentView.backgroundColor = UIColor.white
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM