gpt4 book ai didi

ios - 在我的 Swift 应用程序中使用 AlamofireImages 缓存 UITableViewCells 上的图像

转载 作者:行者123 更新时间:2023-11-30 13:03:58 24 4
gpt4 key购买 nike

我正在编写一个 Swift 应用程序,该应用程序会在 UITableViewController 的每个单元格上显示从服务器获取的照片。

到目前为止,我的代码如下所示:

func tableView(detailsPanel: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell = detailsPanel.dequeueReusableCellWithIdentifier("cell") as! DetailsCell

let test:SingleTest = self.items[indexPath.row] as! SingleTest
if(test.photo != "") {
cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!)
}

}

现在的问题是,并非每个单元格都在 cell.photo 中存储照片。其中一些是空字符串 ("")。在这种情况下,当我快速滚动表格 View 时,我看到那些空的 UIImageView 被其他单元格中的照片填充。

解决这个问题的快速方法似乎是添加一个 else block :

if(test.photo != "") {
cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!)
} //this one below:
else {
cell.myPhoto.image = UIImage(named: "placeholderImg")
}

现在只要没有照片,placeHolderImg就会显示在那里。但是...有没有办法避免它并且不在那里显示任何内容?不显示任何内容是指不显示来自不同单元格的图像?

最佳答案

您正在重复使用单元格,因此单元格仍将使用它已加载的上一个图像,除非您将其设置为 nil 或占位符图像。不过我相信您不需要使用 if 语句。您可以使用 af_setImageWithURL 的 placeholderImage 参数。

cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!, placeholderImage: image)

关于ios - 在我的 Swift 应用程序中使用 AlamofireImages 缓存 UITableViewCells 上的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605663/

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