【发布时间】:2015-04-03 08:33:21
【问题描述】:
我有 20 个单元格的 UICollectionView,其中有一个 imageView。当用户单击正确的单元格时,我会在该单元格中绘制另一个 imageView(我们称之为正确的圆圈)。这工作正常。
但我在UICollectionView 中也有一个标题,我在其中询问他们需要回答的问题。每次单击(等待 2 秒)后,问题应该被更新,我有一个更新标题文本标签的功能,这也可以正常工作。
我的问题是:
为了更新标题文本,我必须调用uicollectionView.reloadData(),每当我这样做时,我在单元格(正确圆圈)中绘制的UIImageView 都会移动到另一个单元格!
我已经尝试了所有我能想到的方法,包括这个帖子中的解决方案: UICollectionView reloadData not functioning properly in iOS 7
但不能让它工作。任何想法都非常受欢迎。理想情况下,我只会刷新标题而不是其余单元格,但似乎没有办法做到这一点。
编辑:基于 cmets 稍微修改代码,现在我在每个单元格中有 2 个图像,其中一个被隐藏,然后显示它,而不是在单击时添加 imageView。
以下是一些相关代码:
override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as CollectionViewIconCell
// bigIcon() just returns the animal's icon image
cell.icon.image = animalsArray[indexPath.row].bigIcon()
return cell
}
override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
// we cannot use a dequeueReusableCellWithReuseIdentifier, we need this
let cell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionViewIconCell
if currentAnimal == self.correctAnimal {
cell.circleImage.image = correctCircleImage
cell.circleImage.hidden = false
correctAnswers++
// nextQuestion() simply loads the text that will go in the Header.label
self.nextQuestion()
self.collectionView!.reloadData()
} else {
self.answerCircle.image = UIImage(named: "wrongCircle")
self.answerCircle.sizeToFit()
cell.addSubview(answerCircle)
self.nextQuestion()
self.collectionView!.reloadData()
}
}
谢谢。
附言
阅读了一些 cmets,也许我没有在新问题上正确设置标题的文本?我从教程中获得了所有这些代码,对 UICollectionView 来说非常新,所以请原谅我的无知,但是有没有更好的方法来刷新标题文本?我无法在 viewForSupplementaryElementOfKind 函数之外获取标题。
我应该解释一下,我有 2 个不同的 imageViews,一个用于正确答案,一个用于错误答案,因为正确答案的圆圈应该保留,而错误答案的圆圈将在每个问题上被删除并重复使用。
【问题讨论】:
-
请显示创建单元格的代码
标签: ios swift uicollectionview