集合视图存储按钮按下Swift

时间:2017-08-06 21:19:31

标签: swift uicollectionview

我有一个集合视图,需要存储按下哪个按钮。这就像是一个测验,他们有选项A,B,C,D。问题是它会用这个代码选择项目:

extension QuestionCell {
func configure(with model: Question) {
    factLabel.text = model.fact
    questionLabel.text = model.question
    revenueLabel.text = model.revenue
    let views = answersStack.arrangedSubviews
    for view in views {
        view.removeFromSuperview()
    }
    for (id, answer) in model.answers {
        print(index)
        let answerLabel = UILabel()
        answerLabel.text = answer

        answersStack.addArrangedSubview(answerLabel)

        let answerButton = UIButton()
       answerButton.tag = index
        let imageNormal = UIImage(named: "circle_empty")
        answerButton.setImage(imageNormal, for: .normal)
        let imageSelected = UIImage(named: "circle_filled")
        answerButton.setImage(imageSelected, for: .selected)
        answerButton.setTitleColor(.black, for: .normal)
        answerButton.addTarget(self, action: #selector(answerPressed(_:)), for: .touchUpInside)
        answersStack.addArrangedSubview(answerButton)

    }
}

但是如果你滑动几次然后回到这个问题,图像将再次变白。我该如何解决这个问题。我还附上了屏幕的样子。

This is how it looks like

〜谢谢

1 个答案:

答案 0 :(得分:0)

你可以用这样的Answer类来解决这个问题。如果你是新手,我应该指出你不会想要在另一个类定义中编写这个代码(比如你的ViewController类。)

class Answer{
    var wasSelected = false
    var text : String

    init(text : String) {
        self.text = text
    }

}
class ButtonWithAnswer : UIButton{
    var answer : Answer?
}

但是现在你的model.answers看起来像这样,并且类型为[String : Answer]

model.answers = ["id1" : Answer(text : "answer 1"), "id2" : Answer(text: "answer 2"), "id3":Answer(text: "answer 3")]

我们的想法是,不是让model.answers包含字符串,而是包含一个新的自定义类型,我们将其命名为Answer,它可以跟踪答案的文本(这是一个名为String类型的文本的属性)以及它是否被选中(这是一个名为wasSelected of bool类型的属性)。你的for循环现在看起来像这样:

for (id, answer) in model.answers {
    let answerLabel = UILabel()
    answerLabel.text = answer.text //this comes from our new class definition

    answersStack.addArrangedSubview(answerLabel)
    //Change this to our new custom button subclass
    let answerButton = ButtonWithAnswer()
    //this line straight up won't work since you got rid of index
    //answerButton.tag = index

    //use this instead
    answerButton.answer = answer
    let imageNormal = UIImage(named: "circle_empty")
    answerButton.setImage(imageNormal, for: .normal)
    let imageSelected = UIImage(named: "circle_filled")
    answerButton.setImage(imageSelected, for: .selected)

    //make it so that any answers that were previously selected are now selected when the view reloads
    answerButton.isSelected = answer.wasSelected


    answerButton.setTitleColor(.black, for: .normal)
    answerButton.addTarget(self, action: #selector(answerPressed(_:)), for: .touchUpInside)
    answersStack.addArrangedSubview(answerButton)


}

这是最后一部分。当我们点击与该答案对应的按钮时,我们需要确保我们的answer.wasSelected等于true。所以在answerPressed中我们应该做这样的事情。

func answerPressed(_ sender : UIButton){
    let answerForButton = (sender as! ButtonWithAnswer).answer!
    answerForButton.wasSelected = true


}

应该覆盖它。如果这看起来像是全新的东西,那么在潜入或向程序员亲自询问之前,您应该在Swift中查找有关类,对象和init方法的教程。但是,我在没有真正亲自到场的情况下向我解释了这一点,并给了你一个完整的演练。