Ich habe ein paar Posts über Probleme mit UICollectionView und iwrong Bilder gefunden, aber alle haben eine gemeinsame - Leute versuchen, Bild in CellForItemAt Methode zu downloaden. Ich habe bereits ein richtiges Bild in meinem App-Bundle und benutze einfach die UIImage (named :) -Methode, um es auf UICollectionViewCell zu setzen. Allerdings bekomme ich immer noch falsche Bilder. Könnte mir jemand helfen?UICollectionView falsches Bild angezeigt
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return liveChannels.count()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ChannelButtonViewCell", for: indexPath) as! ChannelButtonViewCell
let current = liveChannels.channels[indexPath.row]
let currentProgram = current.currentProgram()
let channelKey = current.channel?.channelKey
let programDetail = currentProgram?.name
if let currentProgress = currentProgram?.progress() {
cell.progressView.setProgress(currentProgress, animated: true)
}
print("\(channelKey) - \(current.channel!.logoName)")
cell.imageName = current.channel!.logoName
cell.channelTitleLabel.text = channelKey
cell.channelDetailLabel.text = programDetail
cell.channelButton.addTarget(self, action: #selector(ChannelsViewController.pressed(_:)), for: .primaryActionTriggered)
return cell
}
ich drucke channelKey und logoName zu trösten sicher sein, dass das richtige Bild in der liveChannels.channels Struktur gespeichert ist. (Sie sind in der Tat richtig).
Dies ist meine Zelle:
class ChannelButtonViewCell: UICollectionViewCell {
@IBOutlet weak var progressView: UIProgressView!
@IBOutlet weak var channelButton: ChannelButton!
@IBOutlet weak var channelDetailLabel: UILabel!
@IBOutlet weak var channelTitleLabel: UILabel!
@IBOutlet weak var channelImageView: UIImageView!
var imageName: String?
override func prepareForReuse() {
channelImageView.image = nil
super.prepareForReuse()
}
override func draw(_ rect: CGRect) {
if (self.isSelected) {
if let imageName = imageName {
channelImageView.image = UIImage(named: imageName)
}
} else {
if let imageName = imageName {
channelImageView.image = UIImage(named: imageName.appending("_grey"))
}
}
}
}
Hallo, es sieht aus wie didset den Unterschied machen, jedoch musste ich dort zeichnen, da ich keinen anderen Weg gefunden habe, Bilder basierend auf isSelected Status zu ändern. Haben Sie eine Idee, wo Sie bessere Ergebnisse erzielen können? – BobC
In der Tat verpasste ich solchen Fall, aktualisierte Antwort. –
cool, danke für deine Hilfe, sehr geschätzt. – BobC