首页 > 解决方案 > 集合视图单元格 didHighlightItemAt 背景颜色在调用时不改变颜色

问题描述

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

标签: swiftuicollectionviewuicollectionviewcell

解决方案


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


推荐阅读