存储和调用索引路径Swift

时间:2017-08-07 18:14:48

标签: swift

我在swift中有一个集合视图,它需要存储最后按下哪个按钮。这些按钮由A,B,C,D组成,这就像一个问答游戏。

enter image description here

代码:

extension QuestionCell {
func configure(with model: QuestionCellModel) {
    factLabel.text = model.fact
    questionLabel.text = model.question

    let views = answersStack.arrangedSubviews
    for view in views {
        view.removeFromSuperview()
    }
    for (index, answer) in model.answers.enumerated() {
        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)

    }
}

因此,它可以打印索引,但它应该知道它们选择的最后一个按钮,它们完成时(按钮:A,B,C,D)。根据索引,它应该存储每个集合视图单元格的指定值(测验中有5个问题)。我还没有想出如何解决这个问题的方法,因为我不确定计算机是否会在集合视图中存储多个索引(就像计算机在点击完成时使用测验按钮知道它选择的最后一个索引是什么对于每个问题?)我希望这是有道理的!

谢谢

0 个答案:

没有答案