gpt4 book ai didi

ios - 增加、更新和保存所有单元格 UICollectionView 中的分数

转载 作者:行者123 更新时间:2023-11-28 09:24:17 24 4
gpt4 key购买 nike

我正在使用 Swift 4 创建文字益智游戏,我有 300 个单元格,如何增加、更新和保存所有单元格 UICollectionView 中的分数?

代码:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


if indexPath.row + 1 < EnabledID {

cell.backgroundView = UIImageView(image: UIImage(named: "CorrectMark"))
cell.isUserInteractionEnabled = false
cell.cellLabel.text = ""
QV.score += 2
QV.lblScoreCoins.text = String(score)
UserDefaults.standard.set(lblScoreCoins.text, forKey: "Key")
UserDefaults.standard.synchronize()
lblScoreCoins.text = UserDefaults.standard.string(forKey: "Key")

}

return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let QV : QuestionView = self.storyboard?.instantiateViewController(withIdentifier: "QuestionView") as! QuestionView
QV.idFromCollection = indexPath.row + 1

self.present(QV, animated: true, completion: nil)

QV.lblScoreCoins.text = UserDefaults.standard.string(forKey: "Key")


}

最佳答案

我找到了一个解决方案,在 ViewController 类中写下这段代码:

override func viewDidLoad() {
super.viewDidLoad()

let score = UserDefaults.standard.integer(forKey: "ScoreCoins")
lblScoreCoins.text = String(score)

}

func addScore() {

var score = UserDefaults.standard.integer(forKey: "ScoreCoins")
score += 1
UserDefaults.standard.set(score, forKey: "ScoreCoins")
lblScoreCoins.text = String(score)

}

关于ios - 增加、更新和保存所有单元格 UICollectionView 中的分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47439830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com