Collection 视图单元格 didHighlightItemAt 背景颜色在调用时没有改变颜色

Collection view cell didHighlightItemAt background color isn't changing colors when called

当我的 collection 视图单元格突出显示时,我试图更改背景颜色,但这并没有发生。我最近将 collection 视图从以编程方式创建更改为使用故事板实现,并且在此更改之后 didHighlight 函数不会更改背景颜色。它在自身内部打印语句,但不会改变颜色。

import UIKit
import SnapKit
import RxSwift


class ChannelViewController: UIViewController {
    
    @IBOutlet weak var collectionView: UICollectionView!
    
    
    var data: [Int] = Array(0..<10)
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView.dataSource = self
        self.collectionView.delegate = self
    }
}

extension ChannelViewController: UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView,
                                            numberOfItemsInSection section: Int) -> Int {
        return self.data.count
    }
    
    func collectionView(_ collectionView: UICollectionView,
                                            cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChannelCell.reuseIdentifier, for: indexPath) as! ChannelCell
        let data = self.data[indexPath.item]
        
        cell.channelName.text = String(data)
        cell.channelImage.image = #imageLiteral(resourceName: "stock1")
        cell.layoutIfNeeded()
        cell.channelImage.layer.cornerRadius = cell.channelImage.frame.height/2
        cell.onlineCount.text = "\(Int.random(in: 1...700)) online"
        return cell
    }
}

extension ChannelViewController: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let index = indexPath.item
        print(index)

    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = #colorLiteral(red: 0.8275815845, green: 0.9250754714, blue: 0.999878943, alpha: 1)
        print("Highlighted")
    }
    
    func  collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath)
        cell?.backgroundColor = #colorLiteral(red: 0.9591724277, green: 0.9593101144, blue: 0.9591423869, alpha: 1)
    }
}

extension ChannelViewController: UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView,
                                            layout collectionViewLayout: UICollectionViewLayout,
                                            sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.bounds.width, height: 100)
    }

    func collectionView(_ collectionView: UICollectionView,
                                            layout collectionViewLayout: UICollectionViewLayout,
                                            insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) //.zero
    }

    func collectionView(_ collectionView: UICollectionView,
                                            layout collectionViewLayout: UICollectionViewLayout,
                                            minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }

    func collectionView(_ collectionView: UICollectionView,
                                            layout collectionViewLayout: UICollectionViewLayout,
                                            minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
}

非常简单的修复,我改变了 cell.backgroundColor = //colorcell.contentView.backgroundColor = //color