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()
}
}
}
1条答案
按热度按时间njthzxwz1#
在
prepareForReuse()
方法中重置UICollectionViewCell
的状态可能有效