该应用程序允许用户使用自己的聊天气泡颜色和头像。我的问题是,当我从另一台设备发送消息时,收到消息后,我的SENT单元格气泡颜色和图形表达发生了变化。但案文仍然正确。但是,当我将单元格移出视图并放回原处时,它会更改为正确的颜色/头像。向reloadMessages()添加reloadData没有帮助,只会导致单元格快速闪烁。
这就是我接收消息的方式
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
}
答案 0 :(得分:0)
我认为您必须在retrieveMessages()的末尾添加reloadData方法
self.messageTableView.reloadData()