swift - 如何在 SWIFT 中的 Firebase 存储中正确显示 TableView 中的图片
问题描述
我有一个聊天应用程序,人们可以在其中进行群组交谈,并且每个单元格中都会显示一张小图片以显示谁在说话。我设法从 Firebase 存储中显示这些图片,但并不总是正确的图片显示在正确的位置。
它仅在我转到上一个视图控制器并返回聊天视图以查看每个单元格中正确显示的图片时才有效。
我尝试使用DispatchQueue.main.async {}
可能不是很好,因为它对我不起作用。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let message = messageArray[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
cell.selectionStyle = .none
// CHANGE TEXT ACCORDING TO SENDER
if message.sender == Auth.auth().currentUser?.email{
cell.messageBubble.backgroundColor = UIColor(red:0.30, green:0.68, blue:1.5, alpha:1.0)
// ...
} else {
cell.messageBubble.backgroundColor = UIColor(red:0.94, green:0.94, blue:0.94, alpha:1.0)
// ...
}
let theTimeStamp = messageArray[indexPath.row].createdAt
let doubleTime = Double(theTimeStamp)
let myDate = Date(timeIntervalSince1970: doubleTime )
let dateToShow = myDate.calenderTimeSinceNow()
cell.messageBodyTextView.text = messageArray[indexPath.row].messageBody
cell.usernameLabel.text = messageArray[indexPath.row].name
cell.timeLabel.text = dateToShow
let imagePath = self.storageRef.reference(withPath:"\(message.uid)/resizes/profilImage_150x150.jpg")
imagePath.getData(maxSize: 10 * 1024 * 1024) { (data, error) in
if let error = error {
cell.userPicture.image = UIImage(named: "emptyProfilPic")
cell.userPicture.layer.cornerRadius = cell.userPicture.frame.height / 2
cell.userPicture.clipsToBounds = true
print("Got an error fetching data : \(error.localizedDescription)")
return
}
if let data = data {
cell.userPicture.image = UIImage(data: data)
cell.userPicture.layer.cornerRadius = cell.userPicture.frame.height / 2
cell.userPicture.clipsToBounds = true
}
}
return cell
}
谢谢您的帮助 !
解决方案
您必须通过适当的覆盖 prepareForReuse() 来准备单元格以使其可重用。
对于更干净的代码,我建议您在单独的 cocoa Touch 类中实现单元格,以便更容易覆盖和准备下一个数据传入,避免您的问题。
我的意思是这样的:
class mineCell:UITableViewCell {
@IBOutlet weak var text:UILabel!
@IBOutlet weak var img:UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
func updateCell(dataIn){
.
.
}
override func prepareForReuse() {
text.text = ""
img.image = nil
}
在您的 cellForRowAt 表实现中,只需调用更新函数并像这样传递您的数据:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifier = "mineCell"
if let cell = mineTable.dequeueReusableCell(withIdentifier: identifier, for: indexPath) as? mineCell {
updateCell(dataIn)
return cell
}
return mineCell()
}
通过这种方式,您始终可以确保您的单元格为每次重用做好准备,并且不会从上面的单元格中加载错误的数据。
推荐阅读
- javascript - vscode debug deno 脚本停止 decode.json.dew.js?
- xml - SVG“自动裁剪”算法。命令行首选
- flutter - 颤动PageView onPageChage setstate不起作用
- google-apps-script - ARRAYFORMULA() 以便它在除列的最后一个单元格之外的整个列中工作 - Google 表格
- javascript - 带有 Observables 和 FireAuth 的 Angular 11 Guard
- python - Python中的浮点字符串到IEEE 754 binary128
- powershell - Powershell斜线后两个dota的含义
- python - 按下按钮时如何更新 PyQt5 标签文本?
- javascript - 如果不单击“加载更多”按钮,API 中的对象不会呈现
- mysql - 当没有受影响的行时,如何中断 MySQL 查询的循环?