swift - 集合视图单元格 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
}
}
解决方案
非常简单的修复,我
cell.backgroundColor = //color
改为
cell.contentView.backgroundColor = //color
推荐阅读
- selenium - 如何避免在 Selenium 中有多个 xpath 变量?
- java - 如何从 Spring Initializer 解压缩生成的项目?
- git - 使用 groovy 从 GIT 提交消息中提取 Jira 票证
- reactjs - AntD - 如何为单个组件使用深色主题?
- javascript - 如何制作更新内容区?
- symfony - Symfony 学说:多模式迁移
- javascript - 具有多个条件的 Dynamodb filterExpression 不起作用
- image - 按钮上的 SwiftUI 动画图像
- angular - 如何将分配的 eventHandler 动态更改为特定的输入元素?
- spring - “Long 类型的方法参数缺少 URI 模板变量‘teamLeadId’”