Swift TableViewCell ImageView始终显示不同的数据
我有一个聊天室,想显示用户的个人资料图片。 所有内容都将正确显示,但不会显示用户的个人资料图片。 每次我重新加载视图时,都会看到其他图片 我使用以下行加载Profileimage:Swift TableViewCell ImageView始终显示不同的数据,swift,uitableview,Swift,Uitableview,我有一个聊天室,想显示用户的个人资料图片。 所有内容都将正确显示,但不会显示用户的个人资料图片。 每次我重新加载视图时,都会看到其他图片 我使用以下行加载Profileimage: func showProfilImage(uid: String, completion: @escaping (UIImage) -> Void) { UserApi.shared.observeUser(uid: uid) { (user) in guard let url = UR
func showProfilImage(uid: String, completion: @escaping (UIImage) -> Void) {
UserApi.shared.observeUser(uid: uid) { (user) in
guard let url = URL(string: user.profilImageUrl!) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil, let data = data, let image = UIImage(data: data) else { return }
DispatchQueue.main.async {
let finishedImage = image.withRenderingMode(.alwaysOriginal)
completion(finishedImage)
}
}.resume()
}
}
self.showProfilImage(uid: self.items[indexPath.row].user.uid!) { (image) in
cell.profilePic.image = image
}
然后我调用cellForRowAt
中的showProfilImage
,并使用以下行:
func showProfilImage(uid: String, completion: @escaping (UIImage) -> Void) {
UserApi.shared.observeUser(uid: uid) { (user) in
guard let url = URL(string: user.profilImageUrl!) else { return }
URLSession.shared.dataTask(with: url) { (data, response, error) in
guard error == nil, let data = data, let image = UIImage(data: data) else { return }
DispatchQueue.main.async {
let finishedImage = image.withRenderingMode(.alwaysOriginal)
completion(finishedImage)
}
}.resume()
}
}
self.showProfilImage(uid: self.items[indexPath.row].user.uid!) { (image) in
cell.profilePic.image = image
}
更新
现在我实现了sdWebImage,并使用URL完成了以下操作:
func showProfilImage(uid: String, completion: @escaping (URL) -> Void) {
UserApi.shared.observeUser(uid: uid) { (user) in
guard let url = URL(string: user.profilImageUrl!) else { return }
completion(url)
}
}
这里我称我的showProfilImage
:
self.showProfilImage(uid: self.items[indexPath.row].user.uid!) { (url) in
cell.profilePic.image = nil
cell.profilePic.sd_setImage(with: url, completed: { (_, _, _, _) in
})
}
如果为每个用户正确获取图像 然后,您可能会遇到滚动和退出单元格队列的问题 要解决
UITableViewCell
自定义类中的此类问题,请在每次出列时使用此函数清除UIImageView
override func prepareForReuse() {
super.prepareForReuse()
self.imageView.image = nil //setting the image to nil when get dequeued to reuse
}
阅读有关此功能的更多信息UITableView重用其视图,这意味着当您加载一次图像且单元格离开屏幕时,UITableView将使单元格返回屏幕以显示不同的数据源,但先前加载的图像仍将显示在UIImageView上 添加这行代码
cell.profilePic.image = nil
self.showProfilImage(uid: self.items[indexPath.row].user.uid!) { (image) in
cell.profilePic.image = image
}
您当前的代码已退出队列,并且没有兑现问题,您有权使用此代码 也像
UserApi.shared.observeUser(uid: uid) { (user) in
在
viewdiload
中,不要在cellForRow
中加载无数次(相同)图片,在控制器或模型中填充数据源时,每个用户加载一次。至少使用一些东西来缓存图像。更新了我的问题。我现在实现了sdWebImage,但仍然存在相同的问题。提前感谢您的帮助!我猜observeUser
被调用了很多次,用户配置文件也被更改了?我在代码中只调用了observeUser一次。它将为每个tableViewCell调用(这次我有4个用户/对话)。因此,它将被调用4次。对吗?它将被调用4次,当你滚动一个隐藏和重新被调用的那一个时,考虑所有的这一次,通过启动一个背景调用所有的图像,当VC显示的可能在<代码> VIELDDIOND 中,然后刷新显示轮廓图像的TabLuno需要。只需调用这行代码:let imageUrl=URL(string:self.items[indexath.row].user.profilImageUrl!)cell.profilePic.sd_setImage(with:imageUrl){({,{,},{,},{,}in}
在items中,我拥有所有用户,因此不需要再次加载它们。这个代码没有问题!