我有一个UICollectionView
的20个单元格,其中有imageView
。当用户点击正确的单元格时,我在该单元格中绘制另一个imageView
(一个正确的圆圈我们称之为)。这工作正常。iOS 8 Swift UICollectionView reloadData()导致视图(图像)跳转单元格
但我也有一个标题在UICollectionView
,我问他们需要回答的问题。每次点击后(等待2秒),问题应该更新,我有一个功能,更新标题文本的标签,这也很好。
我的问题是: 为了更新的标题文字,我要叫uicollectionView.reloadData()
,每当我这样做,我在小区(correctCircle)提请UIImageView
被移动到另一个细胞!
我用尽了一切我能想到的,包括在这个线程解决方案: UICollectionView reloadData not functioning properly in iOS 7
但不能让它开始工作。任何想法都是值得欢迎的。理想情况下,我只会刷新标题而不是其他单元格,但似乎没有办法做到这一点。
编辑:基于评论稍微改变代码,现在我有两个图像在每个单元格中,一个隐藏,然后我显示它,而不是在点击后添加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()
}
}
感谢。
PS
读一些评论,也许我不是一个新问题,正确设置为标题的文本?我从教程中获得了所有这些代码,对于UICollectionView来说很新,所以请原谅我的无知,但是有没有更好的方法来刷新标题文本?我无法获得viewForSupplementaryElementOfKind
函数以外的标题。
我应该解释我有2个不同的imageViews,一个用于正确答案,一个用于错误答案,因为正确答案的循环应该保留,而错误答案的答案将在每个问题上被删除并重新使用。
显示创建单元格的代码请 – 2015-04-03 08:39:35