在iOS中循环访问数组

时间:2016-01-26 22:09:53

标签: ios arrays swift core-data nsorderedset

在这个应用程序中,我将一张NSOrdered卡片保存到"主题"核心数据中的实体,以便用户可以自己进行测验。卡片在时间上很好地翻转和拖动,但在更新卡方法中我遇到了一些麻烦。当前卡显示正常,因为我在视图中设置了加载(NSOrderedSet' s数组属性的第一张卡)。当我拖动并更新时,它会立即转到最后一张牌,例如,如果我在牌组中有5张牌,它将从第一张牌开始,然后立即转到第五张牌。更新此方法的最佳方法是什么,以便根据需要循环?

我想我应该传递一个像tableViewDelegate方法一样的索引属性,但是如果有人之前做过这样的事情并且有更好的方式,我非常感激。

感谢您的帮助。

    func updateCard() {

    //cycle through questions here

    for var i = 0; i < (self.subject.cards?.count)!; i++ {

        self.currentCard = self.subject.cards?.array[i] as? Card
        self.draggableView.questionLabel.text = self.currentCard?.question
        self.draggableView.answerLabel.text = self.currentCard?.answer

    }
}

1 个答案:

答案 0 :(得分:0)

目前,当您致电updateCard时,for循环以计算机速度进行计算,您只能看到最后一个索引。

这是一个选项:

在您的班级中,将名为selectedCardIndex的存储变量设置为实现细节,然后在updateCard中增加和更新视图。

var selectedCardIndex = 0

func updateCard() {

    self.selectedCardIndex += 1
    self.currentCard = self.subject.cards?.array[self.selectedCardIndex] as? Card
    self.draggableView.questionLabel.text = self.currentCard?.question
    self.draggableView.answerLabel.text = self.currentCard?.answer

}