聊天信使UITableViewCell,加载消息

时间:2018-05-02 08:28:52

标签: ios swift firebase

我正在尝试使用firebase将消息加载到我的Messenger中,但是,它没有显示。我将我的单元格背景设置为浅灰色,我可以看到它在数据库中的消息数量和浅灰色匹配的单元格中起作用。

我使用的代码是

invalid type promotion

我的firebase数据库如下所示: enter image description here

我的模拟器看起来像

enter image description here

我的故事板:

enter image description here

我的文字没有显示的任何原因???

*更新

我试图配置我的单元格,现在它似乎正朝着正确的方向但我在发送的View上崩溃,因为它崩溃了

  

线程1:致命错误:在展开时出乎意料地发现nil   可选值

我的tableview单元格的代码是

func loadMsg() {
    let toId = user!.id!
    let fromId = Auth.auth().currentUser!.uid
    let ref = Database.database().reference().child("privateMessages").child(fromId).child(toId)
    ref.observe(.value) { (snapshot) in

        if let snapshot = snapshot.children.allObjects as? [DataSnapshot] {

            self.messages.removeAll()

            for data in snapshot {

                let newMsg = Message(dictionary: data.value as! [String: AnyObject])

                self.messages.append(newMsg)

            }
        }

        DispatchQueue.main.async {self.tableView.reloadData()}
    }
}
我正朝着正确的方向前进吗?抱歉不确定在哪里发布我的代码,因此将其发布在答案部分。

1 个答案:

答案 0 :(得分:1)

  

"开始/结束外观转换的不平衡调用"

在单元格显示完成之前尝试显示内容时会发生崩溃。

尝试创建新的自定义单元格xib并尝试。

像这样注册。

self.tableView.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: "TableViewCell")


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell:TableViewCell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! TableViewCell

    let message = messages[indexPath.row]

    cell.configCell(message: message)
    cell.backgroundColor = UIColor.lightGray

    return cell
}
相关问题