简体   繁体   English

dequeueReusableCell 问题。 当我选择一个单元格时,正在选择另一个单元格

[英]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.

相关问题 UICollectionView:当我仅选择一个单元格时,会选择多个单元格 - UICollectionView: issue many cells are selected when I only select one cell 当 Cell 位于自己的 class 中时,我无法 dequeueReusableCell - I can't dequeueReusableCell when the Cell is in its own class Monotouch中的DequeueReusableCell滚动时会导致单元重新排列 - DequeueReusableCell in Monotouch causes cells to rearrange when scrolling 当其中一个单元格被选中时,禁用其他表格视图单元格的文本字段 - Disable textfield for other table view cells, when one of the cell is selected 选择另一个单元时如何取消选择 UITableView 单元? - How to deselect a UITableView Cell when another one is selected? 使用dequeueReusableCell重写单元格 - Using dequeueReusableCell rewrites cells 选择单元格时在 UICollectionView 中调暗其他单元格 - Dimming other cells in UICollectionView when a cell is selected 选择时更改单元格视图的颜色。 并在选择另一个单元时将其更改回 - change the cell view color when it select. And change it back when selected another cell 选择其他单元格时,取消选择集合视图单元格 - Deselect a collectionview cell when another cell is selected 如何在另一个表对象中显示所选单元格的子单元格? - How to show child cells of a selected cell in another table object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM