Problem with UICollectionViewCell Custom

0

I'm doing a questionnaire in ios with swift 3, the problem I have is that when I get to the 4 cell in the collectionView the elements in the UICollectionViewCell are duplicated as shown in the image

this is my UICollectionViewCell code

protocol QuestionDelegate: class {

func nextPage(sender: QuestionViewCell,answersMap: [Int : Int],questionNumber: Int)

}

class QuestionViewCell: UICollectionViewCell {

var currentLanguage: String?
var completeQuestionTwoLanguage: CompleteQuestionTwoLanguages?
var questionNumber: Int!

let itemSize = 70
let xOffset = 30

weak var delegate: QuestionDelegate?

var otherButtons = [DLRadioButton]()
var firstRadioButton = DLRadioButton()

var questionTitle: UILabel!
var answersStack: UIStackView!
var nextButton: UIButton!

// let questionTitle: UILabel = { // let label = UILabel () // label.lineBreakMode = .byWordWrapping // label.font = UIFont.init (name: "ZillaSlab", size: 17.0) // label.numberOfLines = 0 // label.translatesAutoresizingMaskIntoConstraints = false // return label //} ()

// let answersStack: UIStackView = { // let stack = UIStackView () // stack.alignment = .fill // stack.distribution = .fillProportionally // stack.axis = .vertical // stack.translatesAutoresizingMaskIntoConstraints = false // return stack // //} ()

// let nextButton: UIButton = { // let button = UIButton () // button.backgroundColor = UIColor.orange // button.setTitle ("next_button" .localized.uppercased (), for: .normal) // button.setTitleColor (UIColor.white, for: .normal) // button.translatesAutoresizingMaskIntoConstraints = false // return button //} () //

func prepareSubViews(){

    print("prepare subviews")

    questionTitle = {
        let label = UILabel()
        label.lineBreakMode = .byWordWrapping
        label.font = UIFont.init(name: "ZillaSlab", size: 17.0)
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    answersStack = {
        let stack = UIStackView()
        stack.alignment = .fill
        stack.distribution = .fillProportionally
        stack.axis = .vertical
        stack.translatesAutoresizingMaskIntoConstraints = false
        return stack

    }()

    nextButton = {
        let button = UIButton()
        button.backgroundColor = UIColor.orange
        button.setTitle("next_button".localized.uppercased(), for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    firstRadioButton = DLRadioButton()
    otherButtons = [DLRadioButton]()

}

func configure(_ questions: CompleteQuestionTwoLanguages,_ current: String,_ Number: Int) {

    currentLanguage = current
    completeQuestionTwoLanguage = questions
    questionNumber = Number

}

func addViews(){

    self.addSubview(questionTitle!)
    self.addSubview(answersStack!)
    self.addSubview(nextButton!)

    questionTitle?.widthAnchor.constraint(equalToConstant: self.frame.size.width)
    questionTitle?.heightAnchor.constraint(equalToConstant: 80).isActive = true
    questionTitle?.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
    questionTitle?.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: CGFloat(16)).isActive = true
    questionTitle?.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -16).isActive = true

    answersStack?.topAnchor.constraint(equalTo: (questionTitle?.bottomAnchor)!, constant: 2).isActive = true
    answersStack?.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 20).isActive = true
    answersStack?.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -20).isActive = true

    nextButton?.topAnchor.constraint(equalTo: (answersStack?.bottomAnchor)!, constant: CGFloat(16)).isActive = true
    nextButton?.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 100).isActive = true
    nextButton?.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -100).isActive = true
    nextButton?.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -16).isActive = true

}

func changeLanguage(_ language: String){

    currentLanguage = language

// currentQuestion.text = completeQuestionTwoLanguage? .getQuestion (currentLanguage!)         questionTitle? .text = completeQuestionTwoLanguage? .getQuestion (currentLanguage!)

    var index = 0

    if firstRadioButton.isMultipleSelectionEnabled {
        let text = self.completeQuestionTwoLanguage?.getAnswerText(index, currentLanguage!)
        firstRadioButton.setTitle(text, for: .normal)
        index = 1
    }

    for button in otherButtons{
        let text = self.completeQuestionTwoLanguage?.getAnswerText(index, currentLanguage!)
        button.setTitle(text, for: .normal)
        index += 1
    }


}

func setupInfo(){

// currentQuestion.text = self.completeQuestionTwoLanguage? .getQuestion (currentLanguage!)         questionTitle? .text = completeQuestionTwoLanguage? .getQuestion (currentLanguage!)

    self.backgroundColor = UIColor.white

    if (completeQuestionTwoLanguage?.isSingleAnswer())!{
        print("question singler answer \(questionNumber)" )
        if let countQuestions = completeQuestionTwoLanguage?.getAnswersCount(currentLanguage!) {

            let frame = CGRect(x: 0, y: 0, width: 300, height: 10);
            firstRadioButton = createRadioButton(frame: frame, title: "", color: UIColor.orange);

            for i in 0..<countQuestions {

                let frame = CGRect(x: 0, y: 0, width: 300, height: 80);
                let text = self.completeQuestionTwoLanguage?.getAnswerText(i, currentLanguage!)

                let other = createRadioButton(frame: frame, title: text!, color: UIColor.orange);
                other.isIconOnRight = true
                otherButtons.append(other)

            }

            firstRadioButton.otherButtons = otherButtons
            firstRadioButton.isHidden = true

        }

    }else{
        print("question multi answers \(questionNumber!)" )
        if let countQuestions = completeQuestionTwoLanguage?.getAnswersCount(currentLanguage!) {

            let firstItemName = self.completeQuestionTwoLanguage?.getAnswerText(0, currentLanguage!)

            let frame = CGRect(x: 0, y: 0, width: 300, height: 80);
            firstRadioButton = createRadioButton(frame: frame, title: firstItemName!, color: UIColor.orange);

            for radioButton in firstRadioButton.otherButtons{
                radioButton.isSelected = true;
            }

            firstRadioButton.isMultipleSelectionEnabled = true
            firstRadioButton.isIconOnRight = true
            firstRadioButton.isIconSquare = true

            print("multi answer count: \(countQuestions)")

            for i in 1..<countQuestions {

                let frame = CGRect(x: 0, y: 0, width: 300, height: 80);
                let text = self.completeQuestionTwoLanguage?.getAnswerText(i, currentLanguage!)

                let other = createRadioButton(frame: frame, title: text!, color: UIColor.orange);
                other.isIconOnRight = true
                other.isIconSquare = true
                other.isMultipleSelectionEnabled = true;
                otherButtons.append(other)

            }

            firstRadioButton.otherButtons = otherButtons

        }

    }

    nextButton?.addTarget(self, action: #selector(self.nextPressed), for: UIControlEvents.touchUpInside)

}

func nextPressed() {
    print("next button pressed")
    if getAnswers().contains(true){
        print("contains true")
        if let d = self.delegate{
            d.nextPage(sender: self, answersMap: (completeQuestionTwoLanguage?.getAnswerMap(getAnswers()))!, questionNumber: questionNumber)
        }

    }else{
        print("track question")
        let tracker = GAI.sharedInstance().defaultTracker
        let eventTracker: NSObject = GAIDictionaryBuilder.createEvent(
            withCategory: "Questions",
            action: "NoAnswer",
            label: "",
            value: nil).build()
        tracker!.send(eventTracker as! [AnyHashable: Any])
    }

}

func createRadioButton(frame : CGRect, title : String, color : UIColor) -> DLRadioButton {
    let radioButton = DLRadioButton(frame: frame);
    radioButton.titleLabel!.font = UIFont.systemFont(ofSize: 17);
    radioButton.titleLabel?.lineBreakMode = .byWordWrapping
    radioButton.titleLabel?.numberOfLines = 0
    radioButton.setTitle(title, for: .normal);
    radioButton.setTitleColor(UIColor.darkGray, for: .normal);
    radioButton.iconSize = .init(20)
    radioButton.iconColor = color;
    radioButton.indicatorColor = color;
    radioButton.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left;

// self.answerStack.addArrangedSubview (radioButton)         self.answersStack? .addArrangedSubview (radioButton)

    return radioButton;
}

//------------------------Set Vars-------------------------------------------------------------------------------------------------------------

func setQuestionNumber(number: Int){
    self.questionNumber = number
}

func setCurrentLanguage(current: String){
    self.currentLanguage = current
}

func setCompleteQuestionTwoLanguage(completeQuestionTwoLanguage: CompleteQuestionTwoLanguages){
    self.completeQuestionTwoLanguage = completeQuestionTwoLanguage
}

private func getDLRadioButtonOf<T : DLRadioButton>(view:UIView) -> [T] {
    var subviews = [T]()

    for subview in view.subviews {
        subviews += getDLRadioButtonOf(view: subview) as [T]

        if let subview = subview as? T {
            subviews.append(subview)
        }
    }

    return subviews
}

func getAnswers() -> [Bool]{

    var answers = [Bool]()

    if firstRadioButton.isMultipleSelectionEnabled {

        answers.append(firstRadioButton.isSelected)
    }

    for button in otherButtons{
        answers.append(button.isSelected)
    }

    return answers

}

//-----------------------------------------------------------------------------------------------------------------------------------------------
    
asked by Abad Ramírez 09.09.2018 в 05:20
source

0 answers