ios 第一个储存格和最后一个储存格同时变更?

wsewodh2  于 2022-12-05  发布在  iOS
关注(0)|答案(1)|浏览(116)

I have a UICollectionView with 4 buttons in each cell. When I select one of the button on the FIRST cell, the same button gets selected on the LAST cell and vice versa. This problem occurs on the last and first cell ONLY.
I have created a video to demonstrate the problem.
And this is how my ViewController and the UICollectionView looks like (I didnt include everything, like the UI stuff and viewDidLoad):
ViewController:

class QuizViewController: UIViewController {
   private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        
        let collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.register(QuestionView.self, forCellWithReuseIdentifier: "questionCell")
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.isPagingEnabled = true
        collectionView.bounces = false
        
        collectionView.delegate = self
        collectionView.dataSource = self
        
        
        return collectionView
    }()

extension QuizViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        self.questionsIndex = indexPath.row
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return questions.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "questionCell", for: indexPath) as! QuestionView
        cell.configure(with: questions[indexPath.row])
        cell.delegate = self
        
        return cell
    }
}

UICollectionViewCell:

class QuestionView: UICollectionViewCell {

   private var question: Question {
        didSet {
            questionLabel.text = question.question
            answerButton1.setTitle(question.answers[0], for: .normal)
            answerButton2.setTitle(question.answers[1], for: .normal)
            answerButton3.setTitle(question.answers[2], for: .normal)
            answerButton4.setTitle(question.answers[3], for: .normal)
        }
    }

   private lazy var answerButton1: CustomAnswerButton = {
        let button = CustomAnswerButton(answerLabel: "")
        button.addTarget(self, action: #selector(didTapAnswerButton), for: .touchUpInside)
        
        return button
    }() //I have 3 more of this button but with different names, like answerButton2

   public func configure(with question: Question) {
        self.question = question
    }

   @objc private func didTapAnswerButton(_ sender: UIButton) {
        
        answerButton1.backgroundColor = .white
        answerButton2.backgroundColor = .white
        answerButton3.backgroundColor = .white
        answerButton4.backgroundColor = .white
        
        if(sender.isSelected) {
            sender.isSelected = false
            delegate?.didUnselectAnswer()
        } else {
            if(answerButton1.isSelected || answerButton2.isSelected || answerButton3.isSelected || answerButton4.isSelected) {
                delegate?.didUnselectAnswer()
            }
            answerButton1.isSelected = false
            answerButton2.isSelected = false
            answerButton3.isSelected = false
            answerButton4.isSelected = false
            sender.isSelected = true
            sender.backgroundColor = UIColor(red: 162/255, green: 210/255, blue: 255/255, alpha: 1)
            delegate?.didSelectAnswer()
        }
    }
}
njthzxwz

njthzxwz1#

prepareForReuse()方法中重置UICollectionViewCell的状态可能有效

class QuestionView: UICollectionViewCell {

    ...
    ...
    ...
    
    override func prepareForReuse() {
        super.prepareForReuse()
    
        // Reset the state of all the buttons
        answerButton1.backgroundColor = .white
        answerButton1.isSelected = false
    
        answerButton2.backgroundColor = .white
        answerButton2.isSelected = false
    
        answerButton3.backgroundColor = .white
        answerButton3.isSelected = false
    
        answerButton4.backgroundColor = .white
        answerButton4.isSelected = false
    }
}

相关问题