Swift中的枚举滑动

时间:2017-07-17 23:45:18

标签: swift

所以我在功能中有这个,我枚举打印按钮

 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)

    }
}

首先,它看起来像第一张图片,但是当我向集合视图滑动几次时,它看起来像第二张图片。我觉得它与枚举函数有关,但我不确定为什么。对此问题的一些帮助将不胜感激! - 谢谢

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

集合视图重复使用单元格,因此您需要在添加新单元格之前删除answersStack中的所有答案

let views = answersStack.arrangedSubviews
for view in views {
    view.removeFromSuperview()
}

for (index, answer) in model.answers.enumerated() {
    ...
}