首页 > 解决方案 > 调用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
}

在接收消息之前。新装 在接收消息之前。 新装

收到消息后,但请注意文本保持正确。但是我的细胞改变了它的气泡颜色和头像 收到消息后,但请注意文本保持正确。

将单元格移出视图 将单元格移出视图

带回单元格,单元格更改为正确的颜色,头像 带回单元格,单元格更改为正确的颜色,头像

标签: swiftuitableviewfirebase

解决方案


我认为您必须在retrieveMessages() 末尾添加reloadData 方法

self.messageTableView.reloadData()

推荐阅读