#swift #uicollectionview #uicollectionviewcell
#swift #uicollectionview #uicollectionviewcell
Вопрос:
Я пытаюсь изменить цвет фона, когда выделена моя ячейка представления коллекции, но этого не происходит. Недавно я изменил представление коллекции с созданного программно на реализованное с использованием раскадровки, и после этого изменения 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 = //color
на cell.contentView.backgroundColor = //color