gpt4 book ai didi

swift - 在 collectionView swift 3 中滚动后加载错误的图像

转载 作者:行者123 更新时间:2023-11-28 15:55:48 25 4
gpt4 key购买 nike

我在 Swift 3 中使用我的 collectionView 时遇到问题。我在 Stackoverflow 上找到了一些提示,尝试了它们,但遗憾的是无济于事。许多人提到使用“prepareReuse”方法,但我无法在我的代码中使用这些方法。向下滚动并再次返回后,图像发生了变化。所有图像都是字母表中的字母。所以 A、B、C 是最先出现在 View 顶部的图像。如果您向下滚动并返回,一些随机的其他字母将取代它们。我的整个代码如下:

import UIKit

class colViewController2: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

var imageData: [String] = [String]()
var imageCounter: Int = 0
var userHasHieroglyph: NSArray = ["","","C","D","E","F","G","H","I","J","K","L","M","N","O","","Q","R","S","T","U","V","W","X","Y","Z"]

override func viewDidLoad() {
super.viewDidLoad()

imageData = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
for b in imageData {
if userHasHieroglyph.contains(b) {
let newHieroglyph = b.lowercased()
imageData[imageData.index(of: b)!] = "h-"+newHieroglyph
}
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) as! MyImageCell
cell.backgroundColor = UIColor.white
var currImage:String = ""
currImage = self.imageData[self.imageCounter]
self.imageCounter += 1

if self.imageCounter >= self.imageData.count {
self.imageCounter = 0
}

cell.image.image = UIImage(named: currImage)
return cell
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 26
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 90, height: 90)
}
}

我希望我遗漏了一些代码来解决这个问题,但是在 Stackoverflow 和互联网上搜索了很多小时之后,我仍然找不到解决这个问题的方法。如果有人有解决方案或提示,将不胜感激!

问候

最佳答案

问题是您使用 imageCounter 作为图像数组的索引,但增加它而不是使用 indexPath.item。请记住,UICollectionView 将重用 UICollectionViewCell 实例。基本上,它只会为屏幕上的单元创建单元实例。如果一个单元格滚出屏幕,一个新的单元格取而代之(例如,如果屏幕上有“A”、“B”和“C”,向下滚动你会看到“B”、“C”、“D”,UICollectionView 将为“D”重用“A”单元格。这有点过于简单化,但或多或​​少是如何工作的)。因此,cellForItem 调用将为您提供有关在 indexPath 参数中显示哪个单元格的信息。在这种情况下,您可以摆脱所有 self.imageCounter 逻辑,而是执行 cell.image.image = UIImage(named: imageData[indexPath.row])

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellid", for: indexPath) as! MyImageCell
cell.backgroundColor = UIColor.white
cell.image.image = UIImage(named: imageData[indexPath.row])
return cell
}

关于swift - 在 collectionView swift 3 中滚动后加载错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41766723/

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