单元格转换引发异常

时间:2018-10-29 17:37:08

标签: swift uitableview

我试图将信息加载到tableView中,但出现异常,因为单元格中的某些信息在我强制转换时未初始化。这是我的代码:

包含tableView的视图的代码:

    private func populateActiveChats()
        {
            let loggedOnUserID = Auth.auth().currentUser?.uid
            let ref = Constants.refs.databaseChatsLite.child(loggedOnUserID!)

            ref.observe(.value, with:
                    { (snapshot) in

                        for child in snapshot.children.allObjects as! [DataSnapshot]
                        {

                            if (self.chatsDictionary.keys.contains(child.key) == false)
                            {
                                let chatValueDictionary = child.value as? NSDictionary
                                self.AddChatToCollections(chatAsDictionary: chatValueDictionary)
                                self.DispatchQueueFunc()
                            }


                        }
                })
        }


    func AddChatToCollections(chatAsDictionary: NSDictionary!)
            {
                if chatAsDictionary == nil
                {
                    return
                }
                let contactName = 

chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_NAME] as! String
            let newMsgs = chatAsDictionary[Constants.Chat.ChatRoomsLite.NUM_OF_UNREAD_MSGS] as! Int
            let contactID = chatAsDictionary[Constants.Chat.ChatRoomsLite.CONTACT_ID] as! String

            let chatToAdd = PrivateChatLiteObject(chattingWith: contactName, ContactID: contactID, unreadMessages: newMsgs, LastMSG: "")


            chatsDictionary[contactID] = chatToAdd
            chatsIndex.append(contactID)
        }

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


            let indexedID = chatsIndex[indexPath.row]
            cell.ContactName.text = chatsDictionary[indexedID]?.GetContactName()

            cell.ContactID = chatsDictionary[indexedID]?.GetContactID()
            return cell

        }

在我的PrivateChatUITableViewCell中:

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

            //cell.ContactImageView = UIImageView.loadImageUsingUrlString(contactImg)
            let indexedID = chatsIndex[indexPath.row]
            cell.ContactName.text = chatsDictionary[indexedID]?.GetContactName()

            cell.ContactID = chatsDictionary[indexedID]?.GetContactID()
            //cell.PopulateCell()
            return cell

        }

public func PopulateCell()
    {
        let currentID = Constants.refs.currentUserInformation?.uid
        Constants.refs.databaseChatsLite.child(currentID!).child(ContactID!).observeSingleEvent(of: .value, with: {(snapshot) in ...})

}

代码到达Constants.refs.databaseChatsLite.child(currentID!).child(ContactID!)

时崩溃

行,因为未初始化ContactID。

在将单元格投射到PrivateChatUITableViewCell时被调用

我没有更改我的代码,它曾经可以正常工作,所以我不确定什么更改或我做错了什么。我的代码应该在哪里固定?

0 个答案:

没有答案