在 iOS 中循环遍历数组

Cycle through array in iOS

在这个应用程序中,我将 NSOrderedSet 卡片保存到核心数据中的 "Subject" 实体,以便用户可以自己进行测验。卡片在 tinder 中翻转和拖动很时尚,但在更新卡片的方法中我遇到了一些麻烦。当前卡片显示正常,因为我在视图中设置加载(NSOrderedSet 数组 属性 的第一张卡片)。但是,当我拖动并更新时,它会立即转到最后一张卡片,例如,如果我在一副牌中有 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

    }
}

目前,当您调用 updateCard 时,for 循环正在 computer-speed 计算,您只能看到最后一个索引。

这是一种选择:

在您的 class 中,将一个名为 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

}