Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 可重用单元问题。选择一个单元格时,将选择另一个单元格_Ios_Swift_Uitableview - Fatal编程技术网

Ios 可重用单元问题。选择一个单元格时,将选择另一个单元格

Ios 可重用单元问题。选择一个单元格时,将选择另一个单元格,ios,swift,uitableview,Ios,Swift,Uitableview,我希望当我选择一个单元格时,放置另一个图像,并在再次单击时返回到旧图像。它可以工作,但当我选择多个单元格并向下滚动时,它会变得混乱,并选择一些我没有选择的单元格。 要重置什么? 在prepareforuse()中添加什么? 这是我的tableviewcell: import Foundation import UIKit class DownloadTableViewCell: UITableViewCell { @IBOutlet weak var downloadImage:

我希望当我选择一个单元格时,放置另一个图像,并在再次单击时返回到旧图像。它可以工作,但当我选择多个单元格并向下滚动时,它会变得混乱,并选择一些我没有选择的单元格。
要重置什么?
在prepareforuse()中添加什么?

这是我的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

    }
}


下面是我的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()
    }

您必须保持图像的状态,以便在单元格外部显示,因为单元格可以退出队列。在下面的示例中,状态存储在CellSelecteImage属性中。在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)
    }
}

您必须保持图像的状态,以便在单元格外部显示,因为单元格可以退出队列。在下面的示例中,状态存储在CellSelecteImage属性中。在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)
    }
}

在prepareforReuse中发布下载的图像

override func prepareForReuse() {

    super.prepareForReuse()

       isHidden = false
       isSelected = false
       isHighlighted = false
       downloadImage.image = nil

}

在prepareforReuse中发布下载的图像

override func prepareForReuse() {

    super.prepareForReuse()

       isHidden = false
       isSelected = false
       isHighlighted = false
       downloadImage.image = nil

}

您只需要选择一个单元格还是多个?我需要多个单元格您只需要选择一个单元格还是多个?我需要多个单元格没有帮助。它使我所有的图像都为零,但默认情况下,图像应该是“圆圈空白”->我把它放在故事板上。它甚至不能解决我的多重选择问题,也没有帮助。它使我所有的图像都为零,但默认情况下,图像应该是“圆圈空白”->我把它放在故事板上。它甚至不能解决我的多重选择问题。