swift - 调用retrieveMessages() 时消息应用程序的TableView 不更新单元格。斯威夫特 4 和火力基地
问题描述
该应用程序允许用户拥有自己的聊天气泡颜色和头像。我的问题是,当我从另一台设备发送消息并收到消息时,我的 SENT 单元格气泡颜色和头像会发生变化。但文字仍然正确。但是当我将单元格移出视图并将其带回来时,它会变为正确的颜色/头像。将 reloadData 添加到 retrieveMessages() 没有帮助,只会导致单元格快速闪烁。
这就是我接收消息的方式
func retrieveMessages() {
let messageDB = Database.database().reference().child("ChatRooms").child(chatRoomID).child("Messages")
messageDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,Any>
let message = snapshotValue["MessageBody"]! as! String
let sentby = snapshotValue["Sender"]! as! String
let sentUid = snapshotValue["userID"]! as! String
let messageObject = Message()
messageObject.messageBody = message
messageObject.sender = sentby
messageObject.userID = sentUid
messageObject.key = snapshot.key
self.messageArray.append(messageObject)
self.configureTableView()
let indexPath: IndexPath = IndexPath(row: self.messageArray.count - 1, section: 0)
self.messageTableView.insertRows(at: [indexPath], with: .left)
self.scrollToBottom()
}
}
这是我的 cellForRowAt。(我知道它有点长。仍然需要重构我能做的)
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customMessageCell", for: indexPath) as! CustomMessageCell
configureTableView()
let lpgr = UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(gestureReconizer:)))
lpgr.minimumPressDuration = 0.5
lpgr.delaysTouchesBegan = true
lpgr.delegate = self
cell.addGestureRecognizer(lpgr)
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(gestureReconizer:)))
tap.delegate = self
tap.numberOfTapsRequired = 2
tap.delaysTouchesBegan = true
cell.messageBackground.addGestureRecognizer(tap)
let messageIndex = messageArray[indexPath.row]
cell.messageBody.text = messageIndex.messageBody
cell.avatar = self.myAvatar
cell.senderUsername.text = messageIndex.sender
cell.avatarImageView.image = UIImage(named: myAvatar)
cell.selectionStyle = .none
cell.likeButtonContraint.constant = 400
if likedMessagesArray.contains(messageIndex.key) {
cell.likeButton.layer.opacity = 100
cell.likeButtonContraint.constant = 2
cell.likeButton.isHidden = false
} else {
cell.likeButtonContraint.constant = 400
cell.layoutIfNeeded()
}
if self.blockedUsersArray.contains(messageArray[indexPath.row].userID) {
self.messageArray.remove(at: indexPath.row)
self.messageTableView.reloadData()
print("User \(messageIndex.sender) is blocked")
} else {
print("User \(messageIndex.sender) not blocked")
}
var color: String?
color = self.myColor
switch color {
case "red" : cell.messageBackground.backgroundColor = UIColor.flatRed()
case "gray": cell.messageBackground.backgroundColor = UIColor.flatGray()
case "lime": cell.messageBackground.backgroundColor = UIColor.flatLime()
case "mint": cell.messageBackground.backgroundColor = UIColor.flatMint()
case "coffee": cell.messageBackground.backgroundColor = UIColor.flatCoffee()
case "pink": cell.messageBackground.backgroundColor = UIColor.flatPink()
default: cell.messageBackground.backgroundColor = UIColor.gray
}
cell.avatarImageView.image = UIImage(named: self.myAvatar)
cell.avatarImageView.backgroundColor = UIColor.white
cell.messageBackground.layer.opacity = 82
cell.messageBackground.frame.size.width = view.frame.size.width
cell.contentView.transform = CGAffineTransform(scaleX: -1, y: 1)
cell.messageBackground.transform = CGAffineTransform(scaleX: -1, y: 1)
if cell.senderUsername.text != Auth.auth().currentUser!.email {
let messageDB = Database.database().reference().child("Users").child(messageIndex.userID)
messageDB.observe(.value) { (snapshot) in
let snapshotValue = snapshot.value as? NSDictionary
let avatar = snapshotValue!["avatar"]! as! String
cell.avatarImageView.image! = UIImage(named: avatar)!
var color: String?
let chatColor = snapshotValue!["chatColor"] as! String
color = chatColor
switch color {
case "red" : cell.messageBackground.backgroundColor = UIColor.flatRed()
case "gray": cell.messageBackground.backgroundColor = UIColor.flatGray()
case "lime": cell.messageBackground.backgroundColor = UIColor.flatLime()
case "mint": cell.messageBackground.backgroundColor = UIColor.flatMint()
case "coffee": cell.messageBackground.backgroundColor = UIColor.flatCoffee()
case "pink": cell.messageBackground.backgroundColor = UIColor.flatPink()
default: cell.messageBackground.backgroundColor = UIColor.gray
}
}
cell.avatarImageView.backgroundColor = UIColor.white
cell.messageBackground.layer.opacity = 82
cell.contentView.transform = CGAffineTransform(scaleX: 1, y: 1)
cell.messageBackground.transform = CGAffineTransform(scaleX: 1, y: 1)
}
return cell
}
解决方案
我认为您必须在retrieveMessages() 末尾添加reloadData 方法
self.messageTableView.reloadData()
推荐阅读
- scala - scala中的大整数
- ansible - 检查 when 语句中是否定义了嵌套变量
- android - 即使应用程序已经销毁,如何使服务在后台运行?
- python - 如何在 dwg 文件中插入图形?
- python - 我正在尝试将 2 个列表合并到一个列表中,并且在使用 zip 和列表时出现 TypeError
- reactjs - 如何在组件列表中更新组件的道具
- java - 我如何解决有关 TestNG 和 Springboot 的问题
- powershell - 如何从PowerShell中加载的.dll文件返回对象
- python - certbot-auto 调试在 pip 失败没有模块 Python
- javascript - 如何更改函数声明以更改 node.js 中的承诺处理?