Ios 在UICollectionView中显示错误的图像
那里!我需要你的帮助。我在UICollectionView中有UIImage,它位于UITableView中。当我第一次从API获取数据时,它会正确显示图像,但当我开始向下滚动并返回时,它会显示错误的图像。我的代码如下所示:Ios 在UICollectionView中显示错误的图像,ios,swift,uitableview,uicollectionview,Ios,Swift,Uitableview,Uicollectionview,那里!我需要你的帮助。我在UICollectionView中有UIImage,它位于UITableView中。当我第一次从API获取数据时,它会正确显示图像,但当我开始向下滚动并返回时,它会显示错误的图像。我的代码如下所示: func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = c
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell
let post = allPostsOfUserArray[collectionView.tag]
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
}
return cell
}
我的模型如下所示:
class UserContent {
var name = ""
var imageLinks = [UserImages]()
init(name: String, imageLinks: [UserImages]?) {
self.name = name
if imageLinks != nil {
self.imageLinks = imageLinks!
}
}
init() { }
deinit {
imageLinks.removeAll()
}
}
class UserImages {
var imageLink: String?
init(imageLink: String?) {
self.imageLink = imageLink
}
init() { }
deinit {
imageLink = ""
}
}
我在UITableView中做什么
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let index = indexPath.row
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! AllPostsOfUserTableViewCell
cell.collectionView.tag = index
let post = allPostsOfUserArray[index]
if post.imageLinks.count == 0 {
cell.collectionView.isHidden = true
} else {
cell.collectionView.isHidden = false
}
return cell
}
UPD:我将cell.collectionView.reloadData()添加到func tableView(cellForRowAt indexPath)。现在它工作得很好
cell.imageOfAnimalInCollectionView.image = nil
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
}
(未测试,但应能正常工作。您也可以使用sd_image
的方法分配一个空url,其中包含一个占位符图像以仅显示占位符图像。或者只提供一个空url字符串”
,而不包含占位符图像)
无论何时滚动,单元格都会被重新使用,分配给单元格的图像会重新出现,因为它仍在单元格上。
对于要将图像分配给imageView的每个if
,必须使用else
子句删除这些图像
希望这有帮助
(未测试,但应能正常工作。您也可以使用sd_image
的方法分配一个空url,其中包含一个占位符图像以仅显示占位符图像。或者只提供一个空url字符串”
,而不包含占位符图像)
无论何时滚动,单元格都会被重新使用,分配给单元格的图像会重新出现,因为它仍在单元格上。
对于要将图像分配给imageView的每个if
,必须使用else
子句删除这些图像
希望这能有所帮助。您能试试下面的代码吗:
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell
cell.imageOfAnimalInCollectionView.image = UIImage(named: "App-Default")
let post = allPostsOfUserArray[collectionView.tag]
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
}
return cell
}
你能试试这个代码吗
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell
cell.imageOfAnimalInCollectionView.image = UIImage(named: "App-Default")
let post = allPostsOfUserArray[collectionView.tag]
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
}
return cell
}
这个问题类似于比赛条件。 你可以参考我的答案
您可能需要找到另一个模块来下载图像并手动设置为单元格。此问题类似于竞争条件。 你可以参考我的答案
您可能需要找到另一个模块来下载图像并手动设置为cell。您必须编写以下其他条件
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell
let post = allPostsOfUserArray[collectionView.tag]
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
else{
cell.imageOfAnimalInCollectionView = "defaultImage"
}
}
else{
cell.imageOfAnimalInCollectionView = "defaultImage"
}
return cell
}
原因
在退出单元格队列后,
cellectionView
将为所有下一个单元格重复使用创建的单元格,因此,如果您不编写else条件或不使用prepareforeuse
方法,它将始终在其缓存中保存上一个单元格的数据。您必须编写如下的其他条件
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! AllPostsOfUserCollectionViewCell
let post = allPostsOfUserArray[collectionView.tag]
if post.imageLinks.count != 0 {
let imageLink = post.imageLinks[indexPath.row]
if imageLink.imageLink != nil {
let url = URL(string: imageLink.imageLink!)
cell.imageOfAnimalInCollectionView.sd_setImage(with: url!, placeholderImage: UIImage(named: "App-Default"),options: SDWebImageOptions(rawValue: 0), completed: { (image, error, cacheType, imageURL) in
})
}
else{
cell.imageOfAnimalInCollectionView = "defaultImage"
}
}
else{
cell.imageOfAnimalInCollectionView = "defaultImage"
}
return cell
}
原因
在将单元格退出队列后,
cellectionView
将为所有下一个单元格重复使用创建的单元格,因此,如果您不编写else条件或不使用prepareforeuse
方法,它将始终在其缓存中保存上一个单元格的数据。在所有else
中(您没有这样做),您需要删除图像或放置占位符。在所有的else
(您没有这样做)中,您需要删除图像或放置占位符。谢谢,但我仍然有一些问题。我想,也许是因为错误的模型。@DmytroRyshchuk很好,你找到了一个解决方案。尽管如此,我认为将图像设置为nil或占位符图像是一种很好的做法。这将有助于你处理更多的数据谢谢你,但我仍然有一些麻烦。我想,也许是因为错误的模型。@DmytroRyshchuk很好,你找到了一个解决方案。尽管如此,我认为将图像设置为nil或占位符图像是一种很好的做法。这将有助于你处理更多的数据