Я хочу, чтобы при выборе ячейки было добавлено другое изображение, и go возвращается к старому изображению, когда я снова щелкаю по нему. Это работает, но когда я выбираю несколько ячеек и прокручиваю вниз, он становится грязным и выбирает некоторые ячейки, которые я не выбрал. Что сбросить? Что добавить в prepareForReuse ()? Вот моя ячейка tableview:
import Foundation
import UIKit
class DownloadTableViewCell: UITableViewCell {
@IBOutlet weak var downloadImage: UIImageView!
@IBOutlet weak var cardSetLabel: UILabel!
func configureCell(label: String) {
cardSetLabel.text = label
downloadImage.image = UIImage(named: "circle_blank")
}
func changeImage() {
if(downloadImage.image == UIImage(named: "circle_download")){
downloadImage.image = UIImage(named: "circle_blank")
self.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
else {
downloadImage.image = UIImage(named: "circle_download")
self.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
}
}
override func prepareForReuse() {
super.prepareForReuse()
isHidden = false
isSelected = false
isHighlighted = false
}
}
А вот мои методы tableView
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "DownloadTableViewCell", for: indexPath) as! DownloadTableViewCell
cell.cardSetLabel.text = cardsets[indexPath.row].title
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.tableView.cellForRow(at: indexPath) as! DownloadTableViewCell
cell.changeImage()
// self.tableView.indexPathsForSelectedRows?.forEach { index in
// debugPrint("Selected rows", index.row)
// }
tableView.reloadData()
}