简体   繁体   English

如何在Swift iOS中更改自定义“收藏夹视图”单元格的背景颜色?

[英]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 将其写入您的cellForItemAtdidSelectItemAt

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.

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