Swift 背景色为非';不要在呼叫时更改颜色
当我的集合视图单元格高亮显示时,我试图更改背景色,但没有发生这种情况。我最近将集合视图从以编程方式创建更改为使用故事板来实现,在这一更改之后,Swift 背景色为非';不要在呼叫时更改颜色,swift,uicollectionview,uicollectionviewcell,Swift,Uicollectionview,Uicollectionviewcell,当我的集合视图单元格高亮显示时,我试图更改背景色,但没有发生这种情况。我最近将集合视图从以编程方式创建更改为使用故事板来实现,在这一更改之后,didphighlight函数不会更改背景颜色。它在自身内部打印语句,但不会更改颜色 import UIKit import SnapKit import RxSwift class ChannelViewController: UIViewController { @IBOutlet weak var collectionView:
didphighlight
函数不会更改背景颜色。它在自身内部打印语句,但不会更改颜色
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
}
}
导入UIKit
导入SnapKit
导入RxSwift
类ChannelViewController:UIViewController{
@ibvar collectionView:UICollectionView!
变量数据:[Int]=数组(0..Int{
返回self.data.count
}
func collectionView(collectionView:UICollectionView,
cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReuseAbleCell(withReuseIdentifier:ChannelCell.reuseIdentifier,for:indexPath)作为!ChannelCell
让data=self.data[indexPath.item]
cell.channelName.text=字符串(数据)
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))在线”
返回单元
}
}
扩展通道视图控制器:UICollectionViewDelegate{
func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
让index=indexath.item
打印(索引)
}
func collectionView(collectionView:UICollectionView,didHighlightItemAt indexPath:indexPath){
让cell=collectionView.cellForItem(位于:indexPath)
单元格?.backgroundColor=#colorLiteral(红色:0.8275815845,绿色:0.9250754714,蓝色:0.999878943,alpha:1)
打印(“突出显示”)
}
func collectionView(collectionView:UICollectionView,didUnhighlightItemAt indexPath:indexPath){
让cell=collectionView.cellForItem(位于:indexPath)
单元格?.backgroundColor=#colorLiteral(红色:0.9591724277,绿色:0.9593101144,蓝色:0.9591423869,alpha:1)
}
}
扩展通道视图控制器:UICollectionViewDelegateFlowLayout{
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
sizeForItemAt indexPath:indexPath)->CGSize{
返回CGSize(宽度:collectionView.bounds.width,高度:100)
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
插入部分:Int)->UIEdgeInsets{
返回UIEDGEINSET(顶部:0,左侧:0,底部:0,右侧:0)/.0
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
截面上的最小最小材料间距:Int)->CGFloat{
返回0
}
func collectionView(collectionView:UICollectionView,
布局集合视图布局:UICollectionViewLayout,
截面的最小线间距:Int)->CGFloat{
返回0
}
}
非常简单的修复,我改变了
cell.backgroundColor=//color
to
cell.contentView.backgroundColor=//color