ios - 有些单元格不会显示

标签 ios swift firebase firebase-realtime-database

我正在尝试创建一个带有表格 View 的聊天应用程序来显示消息。一切工作正常,只是有些单元格不显示。它并不总是相同的细胞。我正在从 Firebase 数据库获取消息。

我的代码:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if let cell = cellCache.object(forKey: indexPath as AnyObject) as? UITableViewCell{
        return cell
    }

    let cell = messagesTableView.dequeueReusableCell(withIdentifier: "otherMessageCell") as! OtherMessageTableViewCell

    DispatchQueue.global(qos: .userInteractive).async {

        let message = self.messages[indexPath.row]
        let ref = FIRDatabase.database().reference()

        let uid = FIRAuth.auth()?.currentUser?.uid

        if(uid == message.sender) {
            cell.sender.textAlignment = .right
            cell.message.textAlignment = .right
        }else{
            cell.sender.textAlignment = .left
            cell.message.textAlignment = .left
        }

        let uidReference = ref.child("Users").child(message.sender!)

        uidReference.observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let username = dictionary["Username"] as! String
                let imageLink = dictionary["Image Link"] as! String

                cell.sender.text = username
                cell.message.text = message.message
                cell.profileImage.image = nil
                cell.profileImage.loadImageWithURLString(urlString: imageLink)
            }

        }, withCancel: nil)
    }

    DispatchQueue.main.async {
        self.cellCache.setObject(cell, forKey: indexPath as AnyObject)
    }

    return cell
}

示例: enter image description here

希望有人能帮助我。谢谢

最佳答案

我找到了解决方案:

var savedSenders = [Int: String]()
var savedMessages = [Int: String]()
var savedImages = [Int: UIImage]()
var savedAlignments = [Int: NSTextAlignment]()

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = messagesTableView.dequeueReusableCell(withIdentifier: "otherMessageCell") as! OtherMessageTableViewCell

    if(savedSenders[indexPath.row] != nil && savedMessages[indexPath.row] != nil && savedImages[indexPath.row] != nil && savedAlignments[indexPath.row] != nil) {
        cell.sender.textAlignment = savedAlignments[indexPath.row]!
        cell.message.textAlignment = savedAlignments[indexPath.row]!

        cell.sender.text = savedSenders[indexPath.row]
        cell.message.text = savedMessages[indexPath.row]
        cell.profileImage.image = savedImages[indexPath.row]

        return cell
    }

    cell.sender.text = ""
    cell.message.text = ""
    cell.profileImage.image = nil

    DispatchQueue.global(qos: .userInteractive).async {

        let message = self.messages[indexPath.row]
        let ref = FIRDatabase.database().reference()

        let uid = FIRAuth.auth()?.currentUser?.uid

        if(uid == message.sender) {
            cell.sender.textAlignment = .right
            cell.message.textAlignment = .right
            self.savedAlignments.updateValue(.right, forKey: indexPath.row)
        }else{
            cell.sender.textAlignment = .left
            cell.message.textAlignment = .left
            self.savedAlignments.updateValue(.left, forKey: indexPath.row)
        }

        let uidReference = ref.child("Users").child(message.sender!)

        uidReference.observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                let username = dictionary["Username"] as! String
                let imageLink = dictionary["Image Link"] as! String

                cell.sender.text = username
                cell.message.text = message.message
                cell.profileImage.image = nil
                cell.profileImage.loadImageWithURLString(urlString: imageLink)

                    let url = URL(string: imageLink)
                    URLSession.shared.dataTask(with: url!) { (data, response, error) in

                        if(error != nil){
                            print(error as Any)
                            return
                        }

                        DispatchQueue.main.async {
                            if let downloadedImage = UIImage(data: data!) {
                                let image = downloadedImage

                                self.savedSenders.updateValue(username, forKey: indexPath.row)
                                self.savedMessages.updateValue(message.message!, forKey: indexPath.row)
                                self.savedImages.updateValue(image, forKey: indexPath.row)
                            }
                        }
                    }.resume()
            }

        }, withCancel: nil)
    }

    return cell
}

每个单元格的数据都保存到数组中(savedSenders、savedMessages、savedImages 和savedAlignments)。如果我不将其保存到数组中,单元格将必须从 Firebase 加载所有数据,这将花费更长的时间。时间长了的话就不好看了。 我测试了一切并且它有效。

关于ios - 有些单元格不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40703384/

相关文章:

ios - 信号量同步请求 Swift4 永远停止

ios - 后台任务 swift 3.0 iOS

ios - 有效地查询火力地堡

java - Firebase 身份验证 : how to get current user's Password?

android - Firebase 在实时数据库发生变化时发送通知

ios - 自定义对象初始化不存储 NSMutableArray 对象与 addObject :

ios - 无法将搜索栏色调颜色更改为在 iOS 8 中透明

javascript - Cordova 设备插件不允许我访问 "device"属性

ios - 无法在测试中将 App Delegate 转换为 App Delegate

swift - 在 Swift 中从 SubScrollView 中删除 ImageView