[英]dequeueReusableCell problem. When I select one cell, another cells are being selected
I want when I select a cell, to put another image, and go back to old image when I click it again.我希望当我选择一个单元格时,放置另一个图像,并在我再次单击它时返回到旧图像。 It works but when I select multiple cells and scroll down, it gets messy and selects some cells I didnt select.
它有效,但是当我选择多个单元格并向下滚动时,它会变得混乱并选择一些我没有选择的单元格。
What to reset?要重置什么?
What to add in prepareForReuse()?在 prepareForReuse() 中添加什么?
Here is my tableviewcell:这是我的tableviewcell:
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
}
}
And here are my tableView methods这是我的 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()
}
you have to keep a state of an image to display outside of a cell due to fact that a cell can be dequeued.由于单元格可以出列,因此您必须保持图像状态以显示在单元格之外。 In the example below the state is stored in cellSelectedImage property.
在下面的示例中,状态存储在cellSelectedImage属性中。 In tableView(_ :, didSelectRowAt:) you have to add a logic of changing an image for a given row and reload a cell.
在tableView(_ :, didSelectRowAt:) 中,您必须添加更改给定行的图像并重新加载单元格的逻辑。
enum DownloadImage: String {
case circle_blank, circle_download
}
class ViewController: UIViewController {
@IBOutlet var tableView: UITableView!
var data = Array(repeating: "Test", count: 20)
var cellSelectedImage = Array(repeating: DownloadImage.circle_blank.rawValue, count: data.count)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
class CustomCell: UITableViewCell {
@IBOutlet weak var downloadImage: UIImageView!
@IBOutlet weak var cardSetLabel: UILabel!
func configureCell(label: String, imageName: String) {
cardSetLabel.text = label
downloadImage.image = UIImage(named: imageName)
if imageName == DownloadImage.circle_download.rawValue {
self.backgroundColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
} else {
self.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
}
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomCell
cell.configureCell(label: data[indexPath.row], imageName: cellSelectedImage[indexPath.row])
return cell
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let imgName = cellSelectedImage[indexPath.row] == DownloadImage.circle_blank.rawValue ? DownloadImage.circle_download.rawValue : DownloadImage.circle_blank.rawValue
cellSelectedImage[indexPath.row] = imgName
tableView.reloadRows(at: [indexPath], with: .automatic)
}
}
Release downloaded image in prepareforReuse在 prepareforReuse 中释放下载的图像
override func prepareForReuse() {
super.prepareForReuse()
isHidden = false
isSelected = false
isHighlighted = false
downloadImage.image = nil
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.