【发布时间】:2017-11-22 16:40:27
【问题描述】:
我正在使用 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")
}
【问题讨论】:
-
我想在您的数据源集合中,您为每个单元格持有某种类型的数据对象。为每个单元格保存您的分数等,并使单元格显示来自数据对象的分数。然后你所要做的就是调用 reloadData 来更新所有的单元格。
-
不要修改单元格属性,而只修改您的数据源并为任何特定单元格调用 reloadCell。您的所有单元格都没有被使用,而只有显示的单元格在屏幕上被重新使用
-
我该怎么做? @Wasserfloh
-
好的,最好的解决方案是什么? @艾哈迈德
标签: ios swift uicollectionview cell