gpt4 book ai didi

iOS 8 Swift UICollectionView reloadData() 导致 View (图像)跳单元格

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:58 47 4
gpt4 key购买 nike

我有一个包含 20 个单元格的 UICollectionView,其中有一个 imageView。当用户点击正确的单元格时,我会在该单元格中绘制另一个 imageView(我们称之为 correctCircle)。这很好用。

但我在 UICollectionView 中也有一个 header ,我可以在其中提出他们需要回答的问题。每次点击(等待 2 秒)后,问题应该更新,我有一个更新标题文本标签的功能,这也可以正常工作。

我的问题是:为了更新页眉文本,我必须调用 uicollectionView.reloadData(),每当我这样做时,我在单元格(correctCircle)中绘制的 UIImageView 就会得到移动到另一个单元格!

我已经尝试了所有我能想到的,包括这个线程中的解决方案: UICollectionView reloadData not functioning properly in iOS 7

但无法让它工作。任何想法都非常受欢迎。理想情况下,我只会刷新标题而不是其余的单元格,但似乎没有办法做到这一点。

编辑:根据评论稍微修改代码,现在我在每个单元格中有 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()
}
}

谢谢。

附言

阅读一些评论,也许我没有在新问题上正确设置标题文本?我从教程中获得了所有这些代码,对 UICollectionView 非常陌生,所以请原谅我的无知,但是有没有更好的方法来刷新标题文本?我无法在 viewForSupplementaryElementOfKind 函数之外获取 header 。

我应该解释一下我有 2 个不同的 imageView,一个用于正确答案,一个用于错误答案,因为正确答案的圆圈应该保留,而错误答案的圆圈将在每个问题上删除并重新使用。

最佳答案

您的问题不在显示的代码中,而是在请求时返回单元格的代码中,特别是因为您没有通过设置两个 ImageView 的状态来完全更新它。因此,当单元格被重复使用时,它具有来自另一行的错误(旧)设置。

实际上这是一个数据模型和单元格设计问题。该单元格应始终有 2 个 ImageView ,您应该根据需要使其可见或隐藏,并且需要根据您在每次用户选择后更新的数据模型确定,然后重新加载(只要可以更新单元格而不是完全重新加载).

关于iOS 8 Swift UICollectionView reloadData() 导致 View (图像)跳单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29428837/

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