[英]How to change the background color of a custom Collection View cell in swift iOS?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
//let imageView = UIImageView()
//cell.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
cell.layer.borderWidth = 0.5
cell.layer.borderColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor
//cell.placeLabel.tintColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00).cgColor
cell.layer.cornerRadius = 40
cell.layer.masksToBounds = true
print("places\(indexPath.row)")
//cell.placeLabel.text = places[indexPath.row] as! String
cell.placeLabel.text = places[indexPath.row]
cell.placeLabel.textColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
if (indexPath.row == 0){
cell.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
}
}
I created a custom collectionViewCell
. 我创建了一个自定义
collectionViewCell
。 When I click one of the cell it's backgroundColor
should change. 当我单击其中一个单元格时,
backgroundColor
应该更改。 How can I achieve this? 我该如何实现?
I have tried it in didSelectItemItamAt indexpath
method but its not working. 我已经在
didSelectItemItamAt indexpath
方法中尝试过,但是它不起作用。 Please help. 请帮忙。
You can use selectedBackgroundView
property of UICollectionViewCell
for that, inside cellForItemAt indexPath
set that property and now when you select cell it will automatically change the backgroundColor
of that cell. 您可以
UICollectionViewCell
使用UICollectionViewCell
selectedBackgroundView
属性,在cellForItemAt indexPath
设置该属性,现在,当您选择单元格时,它将自动更改该单元格的backgroundColor
。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
//Your other code
//Add code to set selectedBackgroundView property
let view = UIView(frame: cell.bounds)
// Set background color that you want
view.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
cell.selectedBackgroundView = view
return cell
}
Using this now there is no need to change backgroundColor
of cell in didSelectItemAt indexPath
it will work automatically and change backgroundColor
for that selected cell. 使用这个现在也没有必要改变
backgroundColor
细胞的didSelectItemAt indexPath
它会自动工作,改变backgroundColor
为选定的单元格。
Write this in your cellForItemAt
or didSelectItemAt
将其写入您的
cellForItemAt
或didSelectItemAt
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath){
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PlaceCollectionViewCell
if (indexPath.row == 0){
cell.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
cell.contentView.backgroundColor = UIColor(colorLiteralRed: 0.278, green: 0.694, blue: 0.537, alpha: 1.00)
collectionView.reloadItemsAtIndexPaths(indexPath)
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.