集合视图单元格 didHighlightItemAt 背景颜色在调用时不会改变颜色

Posted

技术标签:

【中文标题】集合视图单元格 didHighlightItemAt 背景颜色在调用时不会改变颜色【英文标题】:Collection view cell didHighlightItemAt background color isn't changing colors when called 【发布时间】:2020-11-13 19:58:03 【问题描述】:

当我的集合视图单元格突出显示时,我试图更改背景颜色,但这不会发生。我最近将集合视图从以编程方式创建更改为使用情节提要实现,在此更改之后,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
    

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于集合视图单元格 didHighlightItemAt 背景颜色在调用时不会改变颜色的主要内容,如果未能解决你的问题,请参考以下文章

集合视图单元格显示

集合视图单元格的自动布局行为怪异

如何在选择 tableView 的行单元格(作为集合视图单元格)时显示视图控制器?

带有集合视图的 UITableView 单元格

如何将集合视图单元格设置为所有屏幕的相等行单元格

在集合视图中有包含数据的单元格和空单元格